]> Shamusworld >> Repos - ttedit/blob - src/graphicprimitives.cpp
Fixed a handful of bugs related to clicking and dragging
[ttedit] / src / graphicprimitives.cpp
1 //
2 // Graphics primitives
3 //
4 // Various graphic functions that are slightly more complex than those that
5 // come with various widget libraries.
6 //
7 // by James L. Hammons
8 // (C) 2005 Underground Software
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -------------------------------------------------------------
14 // JLH  03/14/1998  Created this file
15 // JLH  01/20/2005  Converted to use wxWidgets
16 // JLH  08/30/2008  Repurposed file to handle more than just bezier curves
17 //
18
19 #include "graphicprimitives.h"
20
21 double abs(double n)                                                    // Helper function
22 {
23         return (n < 0 ? -n : n);
24 }
25
26
27 //
28 // This function takes three points and draws a curve using a second order
29 // Bezier function.
30 //
31 void Bezier(QPainter &p, point p1, point p2, point p3)
32 {
33         double step = abs(p1.x - p3.x), tmp = abs(p1.y - p3.y);
34         step = (tmp > step ? tmp : step);                       // Get the larger of the two...
35         step = (step > 0 ? 1/step : 1);                         // & convert to valid step value
36         step *= 2.0;                                                            // (double it to draw less...)
37
38         int prevX = (int)p1.x, prevY = (int)p1.y;
39
40         for(double u=0; u<=1; u+=step)
41         {
42                 double _2u = 2*u, _uu = u*u, _2uu = 2*_uu;
43                 double x = (p1.x * (1 - _2u + _uu)) + (p2.x * (_2u - _2uu)) + (p3.x * _uu);
44                 double y = (p1.y * (1 - _2u + _uu)) + (p2.y * (_2u - _2uu)) + (p3.y * _uu);
45
46                 p.drawLine(prevX, prevY, (int)x, (int)y);
47                 prevX = (int)x, prevY = (int)y;
48         }
49
50         p.drawLine(prevX, prevY, (int)p3.x, (int)p3.y);
51 }
52
53
54 //
55 // This is a convenience funtion, using IPoints :-)
56 //
57 void Bezier(QPainter &p, IPoint p1, IPoint p2, IPoint p3)
58 {
59         Bezier(p, point(p1.x, p1.y), point(p2.x, p2.y), point(p3.x, p3.y));
60 }
61
62
63 //
64 // Draw a round dot (5x5, centered on [x, y])
65 //
66 void DrawRoundDot(QPainter &p, int32 x, int32 y)
67 {
68         QPoint pt[8];
69
70         pt[0] = QPoint(x - 1, y - 2);
71         pt[1] = QPoint(x + 1, y - 2);
72         pt[2] = QPoint(x + 2, y - 1);
73         pt[3] = QPoint(x + 2, y + 1);
74         pt[4] = QPoint(x + 1, y + 2);
75         pt[5] = QPoint(x - 1, y + 2);
76         pt[6] = QPoint(x - 2, y + 1);
77         pt[7] = QPoint(x - 2, y - 1);
78
79         p.drawPolygon(pt, 8);
80 }
81
82
83 //
84 // Draw a sqaure dot (5x5, centered on [x, y])
85 //
86 void DrawSquareDot(QPainter &p, int32 x, int32 y)
87 {
88         QPoint pt[4];
89
90         pt[0] = QPoint(x - 2, y - 2);
91         pt[1] = QPoint(x + 2, y - 2);
92         pt[2] = QPoint(x + 2, y + 2);
93         pt[3] = QPoint(x - 2, y + 2);
94
95         p.drawPolygon(pt, 4);
96 }
97
98
99 //
100 // Draw a sqaure dot (nxn, centered on [x, y])
101 //
102 void DrawSquareDotN(QPainter &p, int32 x, int32 y, uint32 n)
103 {
104         QPoint pt[4];
105         uint32 offset = (n - 1) / 2;
106
107         pt[0] = QPoint(x - offset, y - offset);
108         pt[1] = QPoint(x + offset, y - offset);
109         pt[2] = QPoint(x + offset, y + offset);
110         pt[3] = QPoint(x - offset, y + offset);
111
112         p.drawPolygon(pt, 4);
113 }
114
115
116 //
117 // Draw a round dot (nxn, centered on [x, y])
118 //
119 void DrawRoundDotN(QPainter &p, int32 x, int32 y, uint32 n)
120 {
121         int radius = (n / 2) + 1;
122         p.drawEllipse(x - radius, y - radius, n, n);
123 }