]> Shamusworld >> Repos - ttedit/blob - src/graphicprimitives.cpp
6eb84a23770a39552862be9d63b6ee8e6283daa7
[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 // This function takes three points and draws a curve using a second order
28 // Bezier function.
29 //
30 void Bezier(wxDC &dc, point p1, point p2, point p3)
31 {
32         double step = abs(p1.x - p3.x), tmp = abs(p1.y - p3.y);
33         step = (tmp > step ? tmp : step);                       // Get the larger of the two...
34         step = (step > 0 ? 1/step : 1);                         // & convert to valid step value
35
36         wxCoord prevX = (wxCoord)p1.x, prevY = (wxCoord)p1.y;
37
38         for(double u=0; u<=1; u+=step)
39         {
40                 double _2u = 2*u, _uu = u*u, _2uu = 2*_uu;
41                 double x = (p1.x * (1 - _2u + _uu)) + (p2.x * (_2u - _2uu)) + (p3.x * _uu);
42                 double y = (p1.y * (1 - _2u + _uu)) + (p2.y * (_2u - _2uu)) + (p3.y * _uu);
43
44                 dc.DrawLine(prevX, prevY, (wxCoord)x, (wxCoord)y);
45                 prevX = (wxCoord)x, prevY = (wxCoord)y;
46         }
47
48         dc.DrawLine(prevX, prevY, (wxCoord)p3.x, (wxCoord)p3.y);
49 }
50
51 //
52 // Draw a round dot (5x5, centered on [x, y])
53 //
54 void DrawRoundDot(wxDC &dc, int32 x, int32 y)
55 {
56         wxPoint pt[8];
57
58         pt[0].x = x - 1, pt[0].y = y - 2;
59         pt[1].x = x + 1, pt[1].y = y - 2;
60         pt[2].x = x + 2, pt[2].y = y - 1;
61         pt[3].x = x + 2, pt[3].y = y + 1;
62         pt[4].x = x + 1, pt[4].y = y + 2;
63         pt[5].x = x - 1, pt[5].y = y + 2;
64         pt[6].x = x - 2, pt[6].y = y + 1;
65         pt[7].x = x - 2, pt[7].y = y - 1;
66
67         dc.DrawPolygon(8, pt);
68 }
69
70 //
71 // Draw a sqaure dot (5x5, centered on [x, y])
72 //
73 void DrawSquareDot(wxDC &dc, int32 x, int32 y)
74 {
75         wxPoint pt[4];
76
77         pt[0].x = x - 2, pt[0].y = y - 2;
78         pt[1].x = x + 2, pt[1].y = y - 2;
79         pt[2].x = x + 2, pt[2].y = y + 2;
80         pt[3].x = x - 2, pt[3].y = y + 2;
81
82         dc.DrawPolygon(4, pt);
83 }
84
85 //
86 // Draw a sqaure dot (nxn, centered on [x, y])
87 //
88 void DrawSquareDotN(wxDC &dc, int32 x, int32 y, uint32 n)
89 {
90         wxPoint pt[4];
91         uint32 offset = (n - 1) / 2;
92
93         pt[0].x = x - offset, pt[0].y = y - offset;
94         pt[1].x = x + offset, pt[1].y = y - offset;
95         pt[2].x = x + offset, pt[2].y = y + offset;
96         pt[3].x = x - offset, pt[3].y = y + offset;
97
98         dc.DrawPolygon(4, pt);
99 }
100
101 //
102 // Draw a round dot (nxn, centered on [x, y])
103 //
104 void DrawRoundDotN(wxDC &dc, int32 x, int32 y, uint32 n)
105 {
106         dc.DrawCircle(x, y, (n / 2) + 1);
107 }