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