Author Topic: Triangle Dissection  (Read 5059 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Triangle Dissection
« on: January 30, 2020, 05:23:37 pm »
Here is why I needed to know Tangent Circle to a Line, what I really needed was point on line closest (and perpendicular) to a point outside the line.

Code: QB64: [Select]
  1. _TITLE "Triangle Dissection 2 user click" 'B+ 2020-01-29
  2. ' Turn a triangle into a square (and back)
  3. ' 2020-01-30 now for any triangle, oh and swap points around until back to original dissection! nice  :)
  4. ' 2020-01-30 Oh now let user click his own triangle for dissection
  5.  
  6. CONST xmax = 800, ymax = 740, blu = &H880000FF, red = &H88FF0000
  7. SCREEN _NEWIMAGE(xmax, ymax, 32)
  8.  
  9. DIM Ax, Ay, Fx, Fy, Jx, Jy '3 corners A is apex, F and J form iso triangle
  10. DIM Bx, By, Cx, Cy 'midpoint AF and AJ
  11. DIM Gx, Gy, Hx, Hy '1/4 lengths of base
  12. DIM distFJ, aJ ' to calc points G and H
  13. DIM Dx, Dy, Ex, Ey 'two crital points for forming 90 degree angles
  14. DIM D2x, D2y, E2x, E2y, G2x, G2y 'copy points to move as independent blocks
  15. DIM a, cnt, cc 'a = angle in degrees loop counter, cycle counter
  16. DIM tx, ty ' for temp holders to swap points  3 way swap not 2 way
  17. DIM mx(3), my(3), pi, oldMouse 'for mouse user input
  18.  
  19.  
  20. getUserTri:
  21. cc = 0
  22. CLS: CIRCLE (400, 370), 200
  23. WHILE pi < 3 'get 3 mouse clicks
  24.     _PRINTSTRING (5, 5), SPACE$(20)
  25.     _PRINTSTRING (5, 5), "Need 3 clicks inside circle, have" + STR$(pi)
  26.     mx(0) = _MOUSEX: my(0) = _MOUSEY
  27.     IF _MOUSEBUTTON(1) AND oldMouse = 0 THEN 'new mouse down
  28.         IF SQR((mx(0) - 400) ^ 2 + (my(0) - 370) ^ 2) < 200 THEN
  29.             pi = pi + 1
  30.             mx(pi) = mx(0): my(pi) = my(0)
  31.             CIRCLE (mx(pi), my(pi)), 2
  32.         END IF
  33.     END IF
  34.     oldMouse = _MOUSEBUTTON(1)
  35.     _DISPLAY
  36.     _LIMIT 60
  37. Ax = mx(1): Ay = my(1)
  38. Jx = mx(2): Jy = my(2)
  39. Fx = mx(3): Fy = my(3)
  40.  
  41. 'initial triangle
  42. 'Ax = 400: Ay = 200: Fx = 200: Fy = 500: Jx = 600: Jy = 500 'jx = 600, jy = 500
  43.  
  44. restart:
  45. cc = cc + 1
  46. IF cc = 4 THEN pi = 0: GOTO getUserTri
  47.  
  48. Bx = (Ax + Fx) / 2: By = (Ay + Fy) / 2: Cx = (Ax + Jx) / 2: Cy = (Ay + Jy) / 2
  49. distFJ = _HYPOT(Fx - Jx, Fy - Jy)
  50. aJ = _ATAN2(Jy - Fy, Jx - Fx)
  51. Gx = Fx + .25 * distFJ * COS(aJ)
  52. Gy = Fy + .25 * distFJ * SIN(aJ)
  53. Hx = Fx + .75 * distFJ * COS(aJ)
  54. Hy = Fy + .75 * distFJ * SIN(aJ)
  55. circleTangentXY Gx, Gy, Cx, Cy, Bx, By, Dx, Dy
  56. circleTangentXY Gx, Gy, Cx, Cy, Hx, Hy, Ex, Ey
  57. D2x = Dx: D2y = Dy
  58. E2x = Ex: E2y = Ey
  59. G2x = Gx: G2y = Gy
  60.  
  61. 'draw traingle for check
  62. 'ln Ax, Ay, Fx, Fy
  63. 'ln Ax, Ay, Jx, Jy
  64. 'ln Fx, Fy, Jx, Jy
  65. 'ln Gx, Gy, Cx, Cy
  66. 'ln Dx, Dy, Bx, By
  67. 'ln Ex, Ey, Hx, Hy
  68. '_DISPLAY
  69. '_DELAY 1
  70.  
  71. 'draw our starter triangle
  72. fquad Ax, Ay, Cx, Cy, Dx, Dy, Bx, By, blu
  73. fquad Bx, By, D2x, D2y, Gx, Gy, Fx, Fy, blu
  74. fquad Cx, Cy, Jx, Jy, Hx, Hy, Ex, Ey, blu
  75. ftri Hx, Hy, G2x, G2y, E2x, E2y, blu
  76.  
  77. 'start dissection with all points needed
  78. a = 1: cnt = 0
  79. WHILE cnt < 180
  80.     CLS
  81.  
  82.     fquad Ax, Ay, Cx, Cy, Dx, Dy, Bx, By, blu
  83.  
  84.     rotate D2x, D2y, Bx, By, a
  85.     rotate Gx, Gy, Bx, By, a
  86.     rotate Fx, Fy, Bx, By, a
  87.     fquad Bx, By, D2x, D2y, Gx, Gy, Fx, Fy, blu
  88.  
  89.     rotate Jx, Jy, Cx, Cy, -a
  90.     rotate Hx, Hy, Cx, Cy, -a
  91.     rotate Ex, Ey, Cx, Cy, -a
  92.     fquad Cx, Cy, Jx, Jy, Hx, Hy, Ex, Ey, blu
  93.  
  94.     rotate G2x, G2y, Cx, Cy, -a
  95.     rotate E2x, E2y, Cx, Cy, -a
  96.     ftri Hx, Hy, G2x, G2y, E2x, E2y, blu
  97.     _DISPLAY
  98.     _LIMIT 60
  99.     cnt = cnt + 1
  100. cnt = 0
  101. WHILE cnt < 180
  102.     CLS
  103.     fquad Ax, Ay, Cx, Cy, Dx, Dy, Bx, By, blu
  104.     fquad Bx, By, D2x, D2y, Gx, Gy, Fx, Fy, blu
  105.     fquad Cx, Cy, Jx, Jy, Hx, Hy, Ex, Ey, blu
  106.  
  107.     rotate G2x, G2y, Hx, Hy, -a
  108.     rotate E2x, E2y, Hx, Hy, -a
  109.     ftri Hx, Hy, G2x, G2y, E2x, E2y, blu
  110.  
  111.     cnt = cnt + 1
  112.     _DISPLAY
  113.     _LIMIT 60
  114. cnt = 0
  115. WHILE cnt < 180
  116.     CLS
  117.     fquad Ax, Ay, Cx, Cy, Dx, Dy, Bx, By, blu
  118.     fquad Bx, By, D2x, D2y, Gx, Gy, Fx, Fy, blu
  119.     fquad Cx, Cy, Jx, Jy, Hx, Hy, Ex, Ey, blu
  120.  
  121.     rotate G2x, G2y, Hx, Hy, a
  122.     rotate E2x, E2y, Hx, Hy, a
  123.     ftri Hx, Hy, G2x, G2y, E2x, E2y, blu
  124.  
  125.     cnt = cnt + 1
  126.     _DISPLAY
  127.     _LIMIT 60
  128. cnt = 0
  129. WHILE cnt < 180
  130.     CLS
  131.  
  132.     fquad Ax, Ay, Cx, Cy, Dx, Dy, Bx, By, blu
  133.  
  134.     rotate D2x, D2y, Bx, By, -a
  135.     rotate Gx, Gy, Bx, By, -a
  136.     rotate Fx, Fy, Bx, By, -a
  137.     fquad Bx, By, D2x, D2y, Gx, Gy, Fx, Fy, blu
  138.  
  139.     rotate Jx, Jy, Cx, Cy, a
  140.     rotate Hx, Hy, Cx, Cy, a
  141.     rotate Ex, Ey, Cx, Cy, a
  142.     fquad Cx, Cy, Jx, Jy, Hx, Hy, Ex, Ey, blu
  143.  
  144.     rotate G2x, G2y, Cx, Cy, a
  145.     rotate E2x, E2y, Cx, Cy, a
  146.     ftri Hx, Hy, G2x, G2y, E2x, E2y, blu
  147.  
  148.     cnt = cnt + 1
  149.     _DISPLAY
  150.     _LIMIT 60
  151. 'swap points for different dissection
  152. tx = Ax: ty = Ay
  153. Ax = Jx: Ay = Jy
  154. Jx = Fx: Jy = Fy
  155. Fx = tx: Fy = ty
  156. GOTO restart
  157.  
  158.  
  159. SUB rotate (x, y, cx, cy, rAngle) 'replace x, y with new position
  160.     DIM angle, distance
  161.     angle = _ATAN2(y - cy, x - cx)
  162.     distance = ((x - cx) ^ 2 + (y - cy) ^ 2) ^ .5
  163.     x = cx + distance * COS(angle + _D2R(rAngle))
  164.     y = cy + distance * SIN(angle + _D2R(rAngle))
  165.  
  166. SUB circleTangentXY (X1, Y1, X2, Y2, xC, yC, findXperp, findYperp)
  167.     'p1 and p2 form a line, with slop and y intersect y0
  168.     'xC, yC is a circle origin
  169.     'we find X, Y such that line x, y to xC, yC is perpendicular to p1, p2 line that is radius of tangent circle
  170.     DIM slope, y0, A, B
  171.     IF X2 <> X1 THEN
  172.         slope = (Y2 - Y1) / (X2 - X1)
  173.         y0 = slope * (0 - X1) + Y1
  174.         A = slope ^ 2 + 1
  175.         B = 2 * (slope * y0 - slope * yC - xC)
  176.         findXperp = -B / (2 * A)
  177.         findYperp = slope * findXperp + y0
  178.     ELSE
  179.         findXperp = X1
  180.         findYperp = yC
  181.     END IF
  182.  
  183. 'SUB drawLine (x1, y1, x2, y2, K AS _UNSIGNED LONG)
  184. '    slope = (y2 - y1) / (x2 - x1)
  185. '    y0 = slope * (0 - x1) + y1
  186. '    LINE (0, y0)-(_WIDTH, slope * _WIDTH + y0), &HFF0000FF
  187. 'END SUB
  188.  
  189. SUB ln (x1, y1, x2, y2)
  190.     LINE (x1, y1)-(x2, y2)
  191.  
  192. '2019-12-16 fix by Steve saves some time with STATIC and saves and restores last dest
  193. SUB ftri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  194.     DIM D AS LONG
  195.     STATIC a&
  196.     D = _DEST
  197.     IF a& = 0 THEN a& = _NEWIMAGE(1, 1, 32)
  198.     _DEST a&
  199.     _DONTBLEND a& '  '<<<< new 2019-12-16 fix
  200.     PSET (0, 0), K
  201.     _BLEND a& '<<<< new 2019-12-16 fix
  202.     _DEST D
  203.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  204.  
  205. 'update 2019-12-16 needs updated fTri 2019-12-16
  206. 'need 4 non linear points (not all on 1 line) list them clockwise so x2, y2 is opposite of x4, y4
  207. SUB fquad (x1, y1, x2, y2, x3, y3, x4, y4, K AS _UNSIGNED LONG)
  208.     ftri x1, y1, x2, y2, x3, y3, K
  209.     ftri x3, y3, x4, y4, x1, y1, K
  210.  
  211.  

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Triangle Dissection
« Reply #1 on: January 30, 2020, 06:01:20 pm »
That was simply awesome.
You are the Maestro.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Triangle Dissection
« Reply #2 on: January 30, 2020, 07:19:53 pm »
Thanks, That one has been on my bucket list for awhile!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Triangle Dissection
« Reply #3 on: January 30, 2020, 08:39:29 pm »
Satisfying to watch.

I get this kind of question *ALL* the damn time, so it feels weird to be the one to ask it, but here goes:

"Why have you done such math?"

(i.e., whats this used for beyond its own sake?)
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Triangle Dissection
« Reply #4 on: January 30, 2020, 10:32:40 pm »

Offline dajan

  • Newbie
  • Posts: 41
    • View Profile
Re: Triangle Dissection
« Reply #5 on: January 31, 2020, 03:34:23 am »
bplus, i suspect that is a very cool looking proof of some geometric rule, I don't have a clue about.

Offline dajan

  • Newbie
  • Posts: 41
    • View Profile
Re: Triangle Dissection
« Reply #6 on: January 31, 2020, 03:37:49 am »
Sorry for doble post.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Triangle Dissection
« Reply #7 on: January 31, 2020, 06:32:15 am »
Quote
Quote from: STxAxTIC on Yesterday at 08:39:29 PM
Satisfying to watch.

I get this kind of question *ALL* the damn time, so it feels weird to be the one to ask it, but here goes:

"Why have you done such math?"

(i.e., whats this used for beyond its own sake?)

https://books.google.com/books?id=LoFNDQAAQBAJ&pg=PA204&lpg=PA204&dq=beyond+its+own+sake?&source=bl&ots=QdDoeKmMXk&sig=ACfU3U1ibaVnT0X0SruzymYEMDl1k29Zpg&hl=en&sa=X&ved=2ahUKEwjQj9ic8qznAhUJWs0KHQAmC3AQ6AEwBXoECAkQAQ#v=onepage&q=beyond%20its%20own%20sake%3F&f=false

WHAT?
You're not done when it works, you're done when it's right.

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Triangle Dissection
« Reply #8 on: January 31, 2020, 06:59:58 am »
Based on reading a part of that book, I can only fathom that bplus is suggesting we talk about Markov chain text generation - because I'm pretty sure that book doesn't actually have any meaning.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Triangle Dissection
« Reply #9 on: January 31, 2020, 11:24:57 am »
Based on reading a part of that book, I can only fathom that bplus is suggesting we talk about Markov chain text generation - because I'm pretty sure that book doesn't actually have any meaning.

LOL that's a pretty good place to begin if you go in search of meaning.


WHAT?

Sorry I didn't have a short answer at the time so I found some dazzling bull shit to keep you busy. It's from Google search of "beyond it's own sake", you got to admit such a question invites religion or psychology and philosophy or moral answering. Like Danilin, that book is always on the verge of making sense ;-))

Back to question,
Satisfying to watch.

I get this kind of question *ALL* the damn time, so it feels weird to be the one to ask it, but here goes:

"Why have you done such math?"

(i.e., whats this used for beyond its own sake?)

Let me rephrase it to what do people get out of math that you can't get from anywhere else?

Unlike any other material thing, math offers certainty and perfection forever and with computers you can picture it quite beautifully, how else might a mere mortal join with the gods?


bplus, i suspect that is a very cool looking proof of some geometric rule, I don't have a clue about.

Not a proof, more a geometric construction and then animated, just so cool looking (that's all I need to be inspired) that when I first saw this at another forum I wanted to replicate the effect without copying the code, so I at least had to understand each step and put them in order. The idea to swap point labels for drawing constructions occurred to me after I checked to see if any triangle will work and going clockwise or counterclockwise with point clicking (check that out if you haven't, along with obtuse vrs acute triangles).

Turns out the construction does not work for any triangle, I found out last night. ;((
I think you need acute triangles; with obtuse, the angle's are ambiguous  2 possible instead of one and you get extra triangles appearing seemly out of nowhere. ???





« Last Edit: January 31, 2020, 11:39:04 am by bplus »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Triangle Dissection
« Reply #10 on: January 31, 2020, 11:58:44 am »
Quote
Turns out the construction does not work for any triangle, I found out last night. ;((
I think you need acute triangles; with obtuse, the angle's are ambiguous  2 possible instead of one and you get extra triangles appearing seemly out of nowhere. ???

Hm, I didn't find it... But could whatever you saw be caused by a discontinuity across pi/2, 2pi, etc - in your trig functions? If so, there's always a vector equivalent to trig that won't suffer the same problem, supposing it arose there. I love watching this thing go though. Make that into a screensaver - the person won't want to wake the computer up!
« Last Edit: January 31, 2020, 12:01:20 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Triangle Dissection
« Reply #11 on: January 31, 2020, 12:39:54 pm »
Hm, I didn't find it... But could whatever you saw be caused by a discontinuity across pi/2, 2pi, etc - in your trig functions? If so, there's always a vector equivalent to trig that won't suffer the same problem, supposing it arose there. I love watching this thing go though. Make that into a screensaver - the person won't want to wake the computer up!

I am thinking the extra triangles are coming from the geometric problem of solving for missing sides (S) or angles (A) that occurs with obtuse angles. Remember SAS, SSS, AAA... ASA? one of them runs into a problem when the angle opposite the hypotenuse is obtuse and has 2 possible solutions for A and thus for 3rd side. The construction just doesn't work for obtuse. OK, I was happily surprised that points can be done counterclockwise as well as clockwise though you get rotations inward instead of the ideal outward but thanks to alpha coloring we can show that too! Heck I was happy A, F, J didn't have to be in particular order eg A had to be top middle point with F to lower left and J lower right, it was at least that flexible (what I like about geometry, works on any scale and often any set of points).

As far as screen savers go, I think this is too simple to hold one's attention for long. Richard Frost's kitchen sink graphics sampler, sorry I can't remember name, Qwerkey's Pie-In-The Sky, to almost name a couple are nice screen saver material IMO. :) 

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Triangle Dissection
« Reply #12 on: January 31, 2020, 12:54:30 pm »
Ah, I see whatcha mean for obtuse triangles. Seems like that's nobody's fault really. Still such a mesmerizing thing to watch happen.
You're not done when it works, you're done when it's right.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Triangle Dissection
« Reply #13 on: February 01, 2020, 02:40:13 pm »
Just ran this for the family and the response was "Can bplus also develop the math that will put a square peg into a round hole?" Another member quibbed "Only if math lies". From there the discussion disintegrated into realms and dimensions and outlandish theories. With regret bplus, I can never show them one of your creations again, it could trigger the next world war. 

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Triangle Dissection
« Reply #14 on: February 01, 2020, 02:56:02 pm »
LOL
Code: QB64: [Select]
  1.  
  2. CIRCLE (320, 240), 100
  3. r = 100 / SQR(2) - 2 ' <<< math  ;-))
  4. LINE (320 - r, 240 - r)-(320 + r, 240 + r), , BF
  5.  
  6.