December 04, 2020, 10:30:54 AM

### Author Topic: [bb] Filled Rotated Ellipses by Andy_A [ 1+ years ago ]  (Read 668 times)

#### BlitzBot

• Jr. Member
•  • Posts: 1 ##### [bb] Filled Rotated Ellipses by Andy_A [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Filled Rotated Ellipses
Author : Andy_A
Posted : 1+ years ago

Description : They're kind of fast, less than 1 ms/oval on average with locked buffer.

Code :
Code: BlitzBasic
1. ;     Title: Filled Rotated Ellipses
2. ;Programmer: Andy Amaya
3. ;      Date: 2011.06.20
4.
5. ;=======================================================
7. ;http://www.blitzmax.com/codearcs/codearcs.php?code=1200
8. ;=======================================================
9.
10. AppTitle "Filled Rotated Ellipses"
11. Global sw = 800 : sh = 600
12. Graphics sw, sh, 32, 2
13. ClsColor 255,222,173
14.
15. numOvals = 30
16.
17. While MouseHit(1) = 0
18.         Cls
19.         LockBuffer()
20.         st = MilliSecs()
21.         For i = 1 To numOvals
22.                 Color Rand(0,255),Rand(0,255),Rand(0,255)
23.                 ovalWide = Rand(20,300)
24.                 ovalHigh = ovalWide/2
25.                 ovalCentX = Rand(ovalWide/2,sw-ovalWide/2)
26.                 ovalCenty = Rand(ovalWide/2+20,sh-ovalWide/2-20)
27.                 angle# = Rand(0.0,359.0)
28.                 rEllipse(ovalCentX, ovalCentY,ovalWide/2,ovalHigh/2,30, angle, 1)
29.         Next
30.         et = MilliSecs()-st
31.         UnlockBuffer()
32.         Color 0,0,0
33.         Text 5,5,"et: "+et
34.         Text sw/2,sh-15,"R-click for more ovals          L-click to exit",True
35.         Flip
36.         WaitMouse()
37. Wend
38.
39. End
40.
42. ;============================== Rotated Ellipse Function ==============================
43. ; Parameters are:
44. ;
45. ; centerX, centerY coords locate center of ellipse to be plotted
46. ; radius1 is width of ellipse along the X axis
47. ; radius2 is height of ellipse along the Y axis
48. ; segments is number of line segments used to draw the ellipse (minimum of 3)
49. ; angle is number of degrees to rotate the ellipse                         (NOTE: 0 degrees = East)
50. ; filled - default is 0 to draw an unfilled ellipse else draw a filled ellipse
51. ;
52. ; NOTE: This function is dependent on fillTriangle() function to perform the filling
53. ;       of rotated ellipses.
54. ;======================================================================================
55.         Local rca#, rsa#, incSize#, i#, ca#, sa#, x1%, y1%, x2%, y2%
56.
57.         rca = Cos(angle) : rsa = Sin(angle)
58.         If segments < 3.0 Then segments = 3.0
59.         incSize = 360.0/segments
60.         If angle = 0 Then
61.                 x1 = radius1+centerX : y1 = centerY
62.         Else
64.         End If
65. ;       Plot x1, y1
66.         i = incSize
67.         While i <= 360.01
69.                 If angle = 0.0 Then
70.                         x2 = Int(ca + centerX) : y2 = Int(sa + centerY)
71.                 Else
72.                         x2 = Int(rca*ca-rsa*sa +centerX) : y2 = Int(rsa*ca+rca*sa +centerY)
73.                 End If
74.                 ;if mode is zero draw an unfilled ellipse
75.                 If filled = 0 Then
76.                         Line x1, y1, x2, y2
77.                 Else
78.                         ;otherwise use the fillTriangle function to fill current arc segment
79.                         fillTriangle(centerX, centerY, x1, y1, x2, y2)
80.                 End If
81.                 x1 = x2 : y1 = y2
82.                 i = i + incSize
83.         Wend
84. End Function
85.
86. ;=======================================================
88. ;http://www.blitzmax.com/codearcs/codearcs.php?code=1200
89. ;=======================================================
90. Function fillTriangle(x1#,y1#,x2#,y2#,x3#,y3#)
91.         Local slope1#,slope2#,slope3#,x#,y#,length#
92.
93.         ;make sure the triangle coordinates are ordered so that x1 < x2 < x3
94.         If x2 < x1 Then x = x2: y = y2: x2 = x1: y2 = y1: x1 = x: y1 = y
95.         If x3 < x1 Then x = x3: y = y3: x3 = x1: y3 = y1: x1 = x: y1 = y
96.         If x3 < x2 Then x = x3: y = y3: x3 = x2: y3 = y2: x2 = x: y2 = y
97.
98.         If x1 <> x3 Then slope1 = (y3-y1)/(x3-x1)
99.         length = x2 - x1
100.         ;draw the first half of the triangle
101.         If length <> 0 Then
102.                 slope2 = (y2-y1)/(x2-x1)
103.                 For x = 0 To length
104.                         Line x+x1,x*slope1+y1,x+x1,x*slope2+y1
105.                 Next
106.         End If
107.
108.         y = length*slope1+y1
109.         length = x3-x2
110.         ;draw the second half
111.         If length <> 0 Then
112.                 slope3 = (y3-y2)/(x3-x2)
113.                 For x = 0 To length
114.                         Line x+x2,x*slope1+y,x+x2,x*slope3+y2
115.                 Next
116.         End If
117. End Function