### Author Topic: Filled Circles and Ellipses (collaborative)  (Read 1734 times)

#### The Librarian

• Moderator
• Newbie
• Posts: 43 ##### Filled Circles and Ellipses (collaborative)
« on: February 12, 2019, 03:33:52 PM »
Filled Circles and Ellipses

Contributor(s): @bplus, @Pete, @SMcNeill, @STxAxTIC
Source: qb64.org Forum
URL: https://www.qb64.org/forum/index.php?topic=1044.0
Tags: [graphics] [circle] [ellipse]

Description:
We develop four variations on the CIRCLE command in the form of four SUBs:
(i) CircleFill = Filled circle
(ii) EllipseFill = Filled ellipse
(iii) EllipseTilt = Tilted ellipse
(iv) EllipseTiltFill = Tilted and filled ellipse

These works have been optimized for speed and respect for alpha transparency.

Source Code:
Code: QB64: [Select]
1. SCREEN _NEWIMAGE(800, 600, 32)
2.
3. DIM TransRed AS _UNSIGNED LONG
4. DIM TransGreen AS _UNSIGNED LONG
5. DIM TransBlue AS _UNSIGNED LONG
6. TransRed = _RGBA(255, 0, 0, 128)
7. TransGreen = _RGBA(0, 255, 0, 128)
8. TransBlue = _RGBA(0, 0, 255, 128)
9.
10. CALL CircleFill(100, 100, 75, TransRed)
11. CALL CircleFill(120, 120, 75, TransBlue)
12.
13. CALL EllipseFill(550, 100, 150, 75, TransBlue)
14. CALL EllipseFill(570, 120, 150, 75, TransGreen)
15.
16. CALL EllipseTilt(200, 400, 150, 75, 0, TransGreen)
17. CALL EllipseTilt(220, 420, 150, 75, 3.14 / 4, TransRed)
18.
19. CALL EllipseTiltFill(0, 550, 400, 150, 75, 3.14 / 6, TransRed)
20. CALL EllipseTiltFill(0, 570, 420, 150, 75, 3.14 / 4, TransGreen)
21.
22.
23. SUB CircleFill (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
24.     ' CX = center x coordinate
25.     ' CY = center y coordinate
27.     '  C = fill color
32.     Y = 0
33.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
34.     LINE (CX - X, CY)-(CX + X, CY), C, BF
35.     WHILE X > Y
37.         IF RadiusError >= 0 THEN
38.             IF X <> Y + 1 THEN
39.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
40.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
41.             X = X - 1
43.         Y = Y + 1
44.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
45.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
46.
47. SUB EllipseFill (CX AS INTEGER, CY AS INTEGER, a AS INTEGER, b AS INTEGER, C AS _UNSIGNED LONG)
48.     ' CX = center x coordinate
49.     ' CY = center y coordinate
50.     '  a = semimajor axis
51.     '  b = semiminor axis
52.     '  C = fill color
53.     IF a = 0 OR b = 0 THEN EXIT SUB
54.     DIM h2w2 AS _INTEGER64
55.     w2 = a * a
56.     h2 = b * b
57.     h2w2 = h2 * w2
58.     LINE (CX - a, CY)-(CX + a, CY), C, BF
59.     DO WHILE y < b
60.         y = y + 1
61.         x = SQR((h2w2 - y * y * w2) \ h2)
62.         LINE (CX - x, CY + y)-(CX + x, CY + y), C, BF
63.         LINE (CX - x, CY - y)-(CX + x, CY - y), C, BF
64.
65. SUB EllipseTilt (CX, CY, a, b, ang, C AS _UNSIGNED LONG)
66.     '  CX = center x coordinate
67.     '  CY = center y coordinate
68.     '   a = semimajor axis
69.     '   b = semiminor axis
70.     ' ang = clockwise orientation of semimajor axis in radians (0 default)
71.     '   C = fill color
72.     FOR k = 0 TO 6.283185307179586 + .025 STEP .025
73.         i = a * COS(k) * COS(ang) + b * SIN(k) * SIN(ang)
74.         j = -a * COS(k) * SIN(ang) + b * SIN(k) * COS(ang)
75.         i = i + CX
76.         j = -j + CY
77.         IF k <> 0 THEN
78.             LINE -(i, j), C
79.             PSET (i, j), C
80.
81. SUB EllipseTiltFill (destHandle&, CX, CY, a, b, ang, C AS _UNSIGNED LONG)
82.     '  destHandle& = destination handle
83.     '  CX = center x coordinate
84.     '  CY = center y coordinate
85.     '   a = semimajor axis
86.     '   b = semiminor axis
87.     ' ang = clockwise orientation of semimajor axis in radians (0 default)
88.     '   C = fill color
89.     DIM max AS INTEGER, mx2 AS INTEGER, i AS INTEGER, j AS INTEGER
90.     D = _DEST: S = _SOURCE
91.     prc = _RGB32(255, 255, 255, 255)
92.     IF a > b THEN max = a + 1 ELSE max = b + 1
93.     mx2 = max + max
94.     tef& = _NEWIMAGE(mx2, mx2)
95.     _DEST tef&
96.     _SOURCE tef&
97.     FOR k = 0 TO 6.283185307179586 + .025 STEP .025
98.         i = max + a * COS(k) * COS(ang) + b * SIN(k) * SIN(ang)
99.         j = max + a * COS(k) * SIN(ang) - b * SIN(k) * COS(ang)
100.         IF k <> 0 THEN
101.             LINE (lasti, lastj)-(i, j), prc
102.             PSET (i, j), prc
103.         lasti = i: lastj = j
104.     DIM xleft(mx2) AS INTEGER, xright(mx2) AS INTEGER, x AS INTEGER, y AS INTEGER
105.     FOR y = 0 TO mx2
106.         x = 0
107.         WHILE POINT(x, y) <> prc AND x < mx2
108.             x = x + 1
109.         xleft(y) = x
110.         WHILE POINT(x, y) = prc AND x < mx2
111.             x = x + 1
112.         WHILE POINT(x, y) <> prc AND x < mx2
113.             x = x + 1
114.         IF x = mx2 THEN xright(y) = xleft(y) ELSE xright(y) = x
115.     _DEST destHandle&
116.     FOR y = 0 TO mx2
117.         IF xleft(y) <> mx2 THEN LINE (xleft(y) + CX - max, y + CY - max)-(xright(y) + CX - max, y + CY - max), C, BF
118.     _DEST D: _DEST S
119.     _FREEIMAGE tef&
120.

« Last Edit: March 07, 2020, 01:20:43 AM by The Librarian »