transformfunctions.bmx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. Strict
  2. ' run in 800X600
  3. Const CWidth#=800
  4. Const CHeight#=600
  5. Global K# = 50
  6. Global CCenterX#=CWidth/2.0
  7. Global CCenterY#=CHeight/3.0
  8. Global YOFFSET = 128
  9. Global XOFFSET = 0
  10. Global ZOFFSET = 5
  11. ' Return the dot product AB · BC.
  12. Function DotProduct#( Ax#,Ay#,Bx#,By#,Cx#,Cy#)
  13. Local BAx#
  14. Local BAy#
  15. Local BCx#
  16. Local BCy#
  17. ' Get the vectors' coordinates.
  18. BAx = Ax - Bx
  19. BAy = Ay - By
  20. BCx = Cx - Bx
  21. BCy = Cy - By
  22. ' Calculate the dot product.
  23. Return (BAx * BCx + BAy * BCy)
  24. End Function
  25. ' Return the cross product AB x BC.
  26. Function CrossProductLength#( Ax#,Ay#,Bx#,By#,Cx#,Cy#)
  27. Local BAx#
  28. Local BAy#
  29. Local BCx#
  30. Local BCy#
  31. ' Get the vectors' coordinates.
  32. BAx = Ax - Bx
  33. BAy = Ay - By
  34. BCx = Cx - Bx
  35. BCy = Cy - By
  36. ' Calculate the Z coordinate of the cross product.
  37. Return(BAx * BCy - BAy * BCx)
  38. End Function
  39. ' Return the angle ABC.
  40. Function GetAngle#( Ax#,Ay#,Bx#,By#,Cx#,Cy#)
  41. Local dot_product#
  42. Local cross_product#
  43. Local angle#
  44. ' Get the dot product and cross product.
  45. dot_product = DotProduct(Ax, Ay, Bx, By, Cx, Cy)
  46. cross_product = CrossProductLength(Ax, Ay, Bx, By, Cx, Cy)
  47. ' Calculate the angle.
  48. angle = MyATan2(cross_product, dot_product)
  49. If angle = 0 Then angle = 180
  50. ' ...handle if angle > 180 case
  51. ' find point ax2,ay2 - rotated -90 degrees
  52. ' find new angle2
  53. ' if angle2 is > 90, then angle = 360-angle
  54. If angle <> 180
  55. Local px# = ax
  56. Local py# = ay
  57. Local rang = -1
  58. If angle < 90 Then rang = -(179-angle)
  59. TFormR(bx,by, rang, px#,py#)
  60. dot_product = DotProduct(px, py, Bx, By, Cx, Cy)
  61. cross_product = CrossProductLength(px, py, Bx, By, Cx, Cy)
  62. ' Calculate the angle.
  63. Local angle2 = MyATan2(cross_product, dot_product)
  64. If angle2 > 90 Then angle=360-angle
  65. EndIf
  66. Return Abs(angle Mod 360)
  67. End Function
  68. ' Return the angle with tangent opp/hyp.
  69. Function MyATan2#(opp#, adj#)
  70. Local angle#
  71. ' Get the basic angle.
  72. If Abs(adj) < 0.0001 Then
  73. angle = 90
  74. Else
  75. angle = Abs(ATan(opp / adj))
  76. End If
  77. ' See if we are in quadrant 2 or 3.
  78. If adj < 0 Then
  79. angle = 180-angle
  80. End If
  81. ' See if we are in quadrant 3 or 4.
  82. If opp < 0 Then
  83. angle = -angle
  84. End If
  85. ' Return the result.
  86. Return angle
  87. End Function
  88. 'scale
  89. Function TFormSZ#(x#, z#)
  90. z:+ZOFFSET '50
  91. Return (x/(z/K))
  92. EndFunction
  93. ' rotate xr,yr around xc,yc
  94. Function TFormR(xc#,yc#, angle, xr# Var,yr# Var)
  95. Local x# = (xr-xc)
  96. Local y# = (yr-yc)
  97. xr = Cos(angle)*x - Sin(angle)*y
  98. yr = Sin(angle)*x + Cos(angle)*y
  99. xr = xc+xr
  100. yr = yc+yr
  101. End Function
  102. 'scale based on z
  103. Function TForm(x#, y#, z#, x2d# Var, y2d# Var )
  104. z:+ZOFFSET
  105. y:+YOFFSET
  106. x:+XOFFSET
  107. x2d = CCenterX+(x/(z/K))
  108. y2d = CCenterY+(y/(z/K))
  109. EndFunction