### Author Topic: Ellipse Intersect Line  (Read 520 times)

0 Members and 1 Guest are viewing this topic.

#### STxAxTIC

• Library Staff
• Forum Resident
• Posts: 1062
• TOXIC
##### Ellipse Intersect Line
« on: February 01, 2020, 04:22:30 PM »
In response to

https://www.qb64.org/forum/index.php?topic=2132

... all I could think is "why stop at circles when you can do ellipses?"

So this program calculates and plots the intersection points between any ellipse and any line. It's not polished whatsoever - you may break something if you try and tweak it. I didn't spend the time to make this more software-like. That can be on you guys if needed. Here's the hard part though:

Code: QB64: [Select]
1. PAINT (1, 1), 15
2.
3. xorig = 0
4. yorig = 50
5.
6. CALL cline(xorig, yorig, xorig + _WIDTH, yorig, 8)
7. CALL cline(xorig, yorig, xorig + -_WIDTH, yorig, 8)
8. CALL cline(xorig, yorig, xorig, yorig + _HEIGHT, 8)
9. CALL cline(xorig, yorig, xorig, yorig - _HEIGHT, 8)
10.
11. xzoom = 20
12. yzoom = 20
13.
14. ' Line
15. b = -2
16. vy = 1
17. vx = -.25
18. v = SQR(vx ^ 2 + vy ^ 2)
19. vx = vx / v
20. vy = vy / v
21. m = vy / vx
22. FOR alpha = -20 TO 20 STEP .001
23.     x = alpha * vx
24.     y = alpha * vy + b
25.     CALL ccircle(xorig + x * xzoom, yorig + y * yzoom, 1, 1)
26.
27. ' Ellipse
28. x0 = 2
29. y0 = -2
30. ax = 10
31. ay = 10
32. bx = -5
33. by = 5
34. FOR t = 0 TO 6.284 STEP .001
35.     x = x0 + ax * COS(t) + bx * SIN(t)
36.     y = y0 + ay * COS(t) + by * SIN(t)
37.     CALL ccircle(xorig + x * xzoom, yorig + y * yzoom, 1, 4)
38.
39. ' Intersections
40. a2 = ax ^ 2 + ay ^ 2
41. b2 = bx ^ 2 + by ^ 2
42. av = ax * vx + ay * vy
43. bv = bx * vx + by * vy
44. rbx = 0 - x0
45. rby = b - y0
46. adbr = ax * rbx + ay * rby
47. bdbr = bx * rbx + by * rby
48. aa = av ^ 2 / a2 ^ 2 + bv ^ 2 / b2 ^ 2
49. bb = 2 * (av * adbr / a2 ^ 2 + bv * bdbr / b2 ^ 2)
50. cc = adbr ^ 2 / a2 ^ 2 + bdbr ^ 2 / b2 ^ 2 - 1
51. alpha1 = (-bb + SQR(bb ^ 2 - 4 * aa * cc)) / (2 * aa)
52. alpha2 = (-bb - SQR(bb ^ 2 - 4 * aa * cc)) / (2 * aa)
53. x1 = alpha1 * vx
54. x2 = alpha2 * vx
55. y1 = alpha1 * vy + b
56. y2 = alpha2 * vy + b
57. CALL ccircle(xorig + x1 * xzoom, yorig + y1 * yzoom, 10, 1)
58. CALL ccircle(xorig + x2 * xzoom, yorig + y2 * yzoom, 10, 1)
59.
60.
61. SUB cline (x1, y1, x2, y2, col)
62.     LINE (_WIDTH / 2 + x1, -y1 + _HEIGHT / 2)-(_WIDTH / 2 + x2, -y2 + _HEIGHT / 2), col
63.
64. SUB ccircle (x1, y1, r, col)
65.     CIRCLE (_WIDTH / 2 + x1, -y1 + _HEIGHT / 2), r, col
TOXIC

Marked as best answer by STxAxTIC on February 01, 2020, 08:35:47 PM

#### STxAxTIC

• Library Staff
• Forum Resident
• Posts: 1062
• TOXIC
##### Re: Ellipse Intersect Line
« Reply #1 on: February 01, 2020, 06:33:01 PM »
Alright, I made it into "software"... Now you can change everything on the fly. Let me know if any bugs show up. (Note the line is assumed to have infinite length, it's not a ray.)

Code: QB64: [Select]
1.
2. xorig = 0
3. yorig = 0
4.
5. CALL cline(xorig, yorig, xorig + _WIDTH, yorig, 8)
6. CALL cline(xorig, yorig, xorig + -_WIDTH, yorig, 8)
7. CALL cline(xorig, yorig, xorig, yorig + _HEIGHT, 8)
8. CALL cline(xorig, yorig, xorig, yorig - _HEIGHT, 8)
9.
10. xzoom = 20
11. yzoom = 20
12.
13. ' Initialize line
14. b = -2
15. d = 2
16. lineang = .1
17. vx = COS(lineang)
18. vy = SIN(lineang)
19. m = vy / vx
20.
21. ' Initialize ellipse
22. x0 = 2
23. y0 = -2
24. ellipsearg = .2
25. amag = 10
26. ax = amag * COS(ellipsearg)
27. ay = amag * SIN(ellipsearg)
28. bmag = 5
29. bx = bmag * COS(ellipsearg + 3.14 / 2)
30. by = bmag * SIN(ellipsearg + 3.14 / 2)
31.
32.
33.         x = _MOUSEX
34.         y = _MOUSEY
35.         IF ((x > 0) AND (x < _WIDTH) AND (y > 0) AND (y < _HEIGHT)) THEN
36.                 x = _MOUSEX
37.                 y = _MOUSEY
38.                 x0 = (x - _WIDTH / 2) / xzoom
39.                 y0 = (-y + _HEIGHT / 2) / yzoom
40.                 x = _MOUSEX
41.                 y = _MOUSEY
42.                 d = (x - _WIDTH / 2) / xzoom
43.                 b = (-y + _HEIGHT / 2) / yzoom
44.                 lineang = lineang + .01
45.                 vx = COS(lineang)
46.                 vy = SIN(lineang)
47.                 m = vy / vx
48.                 lineang = lineang - .01
49.                 vx = COS(lineang)
50.                 vy = SIN(lineang)
51.                 m = vy / vx
52.
53.         CASE 18432
54.             bmag = bmag + .1
55.             bx = bmag * COS(ellipsearg + 3.14 / 2)
56.             by = bmag * SIN(ellipsearg + 3.14 / 2)
57.         CASE 20480
58.             bmag = bmag - .1
59.             bx = bmag * COS(ellipsearg + 3.14 / 2)
60.             by = bmag * SIN(ellipsearg + 3.14 / 2)
61.         CASE 19200
62.             ellipsearg = ellipsearg - .1
63.             ax = amag * COS(ellipsearg)
64.             ay = amag * SIN(ellipsearg)
65.             bx = bmag * COS(ellipsearg + 3.14 / 2)
66.             by = bmag * SIN(ellipsearg + 3.14 / 2)
67.         CASE 19712
68.             ellipsearg = ellipsearg + .1
69.             ax = amag * COS(ellipsearg)
70.             ay = amag * SIN(ellipsearg)
71.             bx = bmag * COS(ellipsearg + 3.14 / 2)
72.             by = bmag * SIN(ellipsearg + 3.14 / 2)
73.
74.     ' Intersections
75.     a2 = ax ^ 2 + ay ^ 2
76.     b2 = bx ^ 2 + by ^ 2
77.     av = ax * vx + ay * vy
78.     bv = bx * vx + by * vy
79.     rbx = d - x0
80.     rby = b - y0
81.     adbr = ax * rbx + ay * rby
82.     bdbr = bx * rbx + by * rby
83.     aa = av ^ 2 / a2 ^ 2 + bv ^ 2 / b2 ^ 2
84.     bb = 2 * (av * adbr / a2 ^ 2 + bv * bdbr / b2 ^ 2)
85.     cc = adbr ^ 2 / a2 ^ 2 + bdbr ^ 2 / b2 ^ 2 - 1
86.     arg = bb ^ 2 - 4 * aa * cc
87.     IF (arg > 0) THEN
88.         alpha1 = (-bb + SQR(arg)) / (2 * aa)
89.         alpha2 = (-bb - SQR(arg)) / (2 * aa)
90.         x1 = alpha1 * vx + d
91.         x2 = alpha2 * vx + d
92.         y1 = alpha1 * vy + b
93.         y2 = alpha2 * vy + b
94.         x1 = -999
95.         y1 = -999
96.         x2 = -999
97.         y2 = -999
98.
99.     GOSUB draweverything
100.
101.     _LIMIT 60
102.
103.
104. draweverything:
105. PAINT (1, 1), 15
106. COLOR 0, 15
107. LOCATE 1, 1: PRINT "LClick=Move ellipse, RClick=Move line, Scroll=Tilt line, Arrows=Shift ellipse"
108. FOR alpha = -20 TO 20 STEP .001
109.     x = alpha * vx + d
110.     y = alpha * vy + b
111.     CALL ccircle(xorig + x * xzoom, yorig + y * yzoom, 1, 1)
112. FOR t = 0 TO 6.284 STEP .001
113.     x = x0 + ax * COS(t) + bx * SIN(t)
114.     y = y0 + ay * COS(t) + by * SIN(t)
115.     CALL ccircle(xorig + x * xzoom, yorig + y * yzoom, 1, 4)
116. CALL ccircle(xorig + x1 * xzoom, yorig + y1 * yzoom, 10, 1)
117. CALL ccircle(xorig + x2 * xzoom, yorig + y2 * yzoom, 10, 1)
118.
119. SUB cline (x1, y1, x2, y2, col)
120.     LINE (_WIDTH / 2 + x1, -y1 + _HEIGHT / 2)-(_WIDTH / 2 + x2, -y2 + _HEIGHT / 2), col
121.
122. SUB ccircle (x1, y1, r, col)
123.     CIRCLE (_WIDTH / 2 + x1, -y1 + _HEIGHT / 2), r, col
« Last Edit: February 01, 2020, 06:47:56 PM by STxAxTIC »
TOXIC

#### bplus

• Forum Resident
• Posts: 6858
• b = b + ...
##### Re: Ellipse Intersect Line
« Reply #2 on: February 02, 2020, 12:18:08 AM »
Ah, having done similar, something I can understand and thus appreciate,

A+ man!

#### STxAxTIC

• Library Staff
• Forum Resident
• Posts: 1062
• TOXIC
##### Re: Ellipse Intersect Line
« Reply #3 on: February 02, 2020, 08:06:50 PM »
For completeness, attached is the derivation of the math enclosed above.

To re-derive what's going on in bplus's code next door, let a=b=r and simplify.
« Last Edit: February 02, 2020, 08:30:35 PM by STxAxTIC »
TOXIC