-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.cpp
executable file
·61 lines (54 loc) · 1.88 KB
/
graph.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
//additional functions for QGraphicsScene
#include "graph.h"
#include <QGraphicsRectItem>
QGraphicsItem * imagelinethick(QGraphicsScene *image, float x1, float y1, float x2, float y2, int thick = 1){
QBrush brush (Qt::black);
QGraphicsItem* item;
if (thick == 1) {
item = image->addLine(x1, y1, \
x2, y2);
return item;
//imageline($image, $x1, $y1, $x2, $y2, $color);
}
float t = thick / 2 - 0.5;
if ( (x1 == x2) || (y1 == y2)) {
QGraphicsRectItem* myitem = image->addRect(round(min(x1, x2) - t), \
round(min(y1, y2) - t), \
round(max(x1, x2) + t) - (round(min(x1, x2) - t)), \
round(max(y1, y2) + t) - (round(min(y1, y2) - t)) );
myitem->setBrush(brush);
return myitem;
/*imagefilledrectangle($image, round(min($x1, $x2) - $t), \
round(min($y1, $y2) - $t), \
round(max($x1, $x2) + $t), \
round(max($y1, $y2) + $t), $color);
*/
}
float k = (y2 - y1) / (x2 - x1); //y = kx + q
float a = t / sqrt(1 + pow(k, 2));
QPolygonF polygon;
polygon << QPointF(round(x1 - (1+k)*a), round(y1 + (1-k)*a)) \
<< QPointF(round(x1 - (1-k)*a), round(y1 - (1+k)*a)) \
<< QPointF(round(x2 + (1+k)*a), round(y2 - (1-k)*a)) \
<< QPointF(round(x2 + (1-k)*a), round(y2 + (1+k)*a));
QGraphicsPolygonItem* pol = image->addPolygon(polygon);
pol->setBrush(brush);
return pol;
}
float max(float x1, float x2) {
if (x1>x2) {
return x1;
} else {
return x2;
}
}
float min(float x1, float x2) {
if (x1<x2) {
return x1;
} else {
return x2;
}
}
float round(float x) {
return x;
}