-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsphere.cpp
50 lines (42 loc) · 1.29 KB
/
sphere.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "sphere.h"
#include "scene3d.h"
#include <QPainter>
#include <cmath>
Sphere::Sphere(int x, int y, int z, int r, Scene3D *scene, QGraphicsItem *parent)
:QGraphicsItem(parent), x(x), y(y), z(z), r(r), scene(scene)
{
}
QPainterPath Sphere::shape() const
{
return path;
}
QRectF Sphere::boundingRect() const
{
const int thickness = this->scene->getThickness();
const int len = thickness * r;
QPoint topLeft = scene->toScenePos(scene->to2D(x, y, z));
topLeft.setX(topLeft.x() - len - thickness);
topLeft.setY(topLeft.y() - len - thickness);
return QRectF(topLeft, QSize(len * 2 + thickness * 3, len * 2 + thickness * 3));
}
void Sphere::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
path = QPainterPath();
drawSphere();
painter->fillPath(path, brush);
}
void Sphere::drawPixel(const QPoint &point)
{
const int offx = this->scene->getOffx();
const int offy = this->scene->getOffy();
const int thickness = this->scene->getThickness();
path.addRect((point.x() + offx) * thickness, (offy - point.y()) * thickness, thickness, thickness);
}
void Sphere::drawSphere()
{
for(const auto &point : Drawer::drawSphere(x, y, z, r, scene->to2D)){
drawPixel(point);
}
}