4 // Various graphic functions that are slightly more complex than those that
5 // come with various widget libraries.
8 // (C) 2005 Underground Software
10 // JLH = James L. Hammons <jlhamm@acm.org>
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
19 #include "graphicprimitives.h"
21 double abs(double n) // Helper function
23 return (n < 0 ? -n : n);
27 // This function takes three points and draws a curve using a second order
30 void Bezier(QPainter &p, point p1, point p2, point p3)
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 step *= 2.0; // (double it to draw less...)
37 int prevX = (int)p1.x, prevY = (int)p1.y;
39 for(double u=0; u<=1; u+=step)
41 double _2u = 2*u, _uu = u*u, _2uu = 2*_uu;
42 double x = (p1.x * (1 - _2u + _uu)) + (p2.x * (_2u - _2uu)) + (p3.x * _uu);
43 double y = (p1.y * (1 - _2u + _uu)) + (p2.y * (_2u - _2uu)) + (p3.y * _uu);
45 p.drawLine(prevX, prevY, (int)x, (int)y);
46 prevX = (int)x, prevY = (int)y;
49 p.drawLine(prevX, prevY, (int)p3.x, (int)p3.y);
53 // Draw a round dot (5x5, centered on [x, y])
55 void DrawRoundDot(QPainter &p, int32 x, int32 y)
59 pt[0] = QPoint(x - 1, y - 2);
60 pt[1] = QPoint(x + 1, y - 2);
61 pt[2] = QPoint(x + 2, y - 1);
62 pt[3] = QPoint(x + 2, y + 1);
63 pt[4] = QPoint(x + 1, y + 2);
64 pt[5] = QPoint(x - 1, y + 2);
65 pt[6] = QPoint(x - 2, y + 1);
66 pt[7] = QPoint(x - 2, y - 1);
72 // Draw a sqaure dot (5x5, centered on [x, y])
74 void DrawSquareDot(QPainter &p, int32 x, int32 y)
78 pt[0] = QPoint(x - 2, y - 2);
79 pt[1] = QPoint(x + 2, y - 2);
80 pt[2] = QPoint(x + 2, y + 2);
81 pt[3] = QPoint(x - 2, y + 2);
87 // Draw a sqaure dot (nxn, centered on [x, y])
89 void DrawSquareDotN(QPainter &p, int32 x, int32 y, uint32 n)
92 uint32 offset = (n - 1) / 2;
94 pt[0] = QPoint(x - offset, y - offset);
95 pt[1] = QPoint(x + offset, y - offset);
96 pt[2] = QPoint(x + offset, y + offset);
97 pt[3] = QPoint(x - offset, y + offset);
103 // Draw a round dot (nxn, centered on [x, y])
105 void DrawRoundDotN(QPainter &p, int32 x, int32 y, uint32 n)
107 int radius = (n / 2) + 1;
108 p.drawEllipse(x - radius, y - radius, n, n);