-
Notifications
You must be signed in to change notification settings - Fork 6
/
triangle.cpp
66 lines (50 loc) · 1.79 KB
/
triangle.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "triangle.h"
#include <QPainter>
Triangle::Triangle(QPointF point,QColor color,int LineWeight, QColor fillColor, QObject *parent) :
Figure(point,parent)
{
/*
* Setting the color and Line weight and then make the name
* Accoding to a counter to the class shapes (tCount)
* Then Set the perimeter to the sum of the triangle sides
*/
Q_UNUSED(point)
shapeColor=color;
this->LineWeight=LineWeight;
this->name=QString("Triangle %1").arg(tCount);
tCount++;
this->perimeter=side1+side2+side3;
this->fillColor = fillColor;
}
Triangle::~Triangle()
{
}
void Triangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
painter->setPen(QPen(shapeColor, LineWeight));
if(fillColor != Qt::white){
painter->setBrush(Qt::SolidPattern);
painter->setBrush(fillColor);
}
else{
painter->setBrush(Qt::NoBrush);
}
QPolygonF polygon;
shapeTypeName = "Triangle";
QPointF p1(startPoint().x() + (endPoint().x() > startPoint().x() ? + 1 : - 1)*
abs((endPoint().x() - startPoint().x())/2), startPoint().y());
QPointF p2((endPoint().x() > startPoint().x()) ? endPoint().x() : startPoint().x(),
endPoint().y());
QPointF p3((endPoint().x() > startPoint().x()) ? startPoint().x() : endPoint().x(),
endPoint().y());
polygon <<p1<<p2<< p3;
QLineF line1(p1.x(),p1.y(),p2.x(),p2.y());
QLineF line2(p1.x(),p1.y(),p3.x(),p3.y());
QLineF line3(p2.x(),p2.y(),p2.x(),p2.y());
this->side1=line1.length();
this->side2=line2.length();
this->side3=line3.length();
this->perimeter=side1+side2+side3;
painter->drawPolygon(polygon);
Q_UNUSED(option)
Q_UNUSED(widget)
}