code_junkie

How do you find a point a given perpendicular distance from a line?

Nov 11th, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. dx = x1-x2
  2. dy = y1-y2
  3. dist = sqrt(dx*dx + dy*dy)
  4. dx /= dist
  5. dy /= dist
  6. x3 = x1 + (N/2)*dy
  7. y3 = y1 - (N/2)*dx
  8. x4 = x1 - (N/2)*dy
  9. y4 = y1 + (N/2)*dx
  10.  
  11. vx = x2-x1
  12. vy = y2-y1
  13. len = sqrt( vx*vx + vy*vy )
  14. ux = -vy/len
  15. uy = vx/len
  16.  
  17. x3 = x1 + N/2 * ux
  18. Y3 = y1 + N/2 * uy
  19.  
  20. x4 = x1 - N/2 * ux
  21. Y4 = y1 - N/2 * uy
  22.  
  23. in: line_length, cap_length, rotation, position of line centre
  24.  
  25. define points:
  26. tl (-line_length/2, cap_length)
  27. tr (line_length/2, cap_length)
  28. bl (-line_length/2, -cap_length)
  29. br (line_length/2, -cap_length)
  30.  
  31. rotate the four points by 'rotation'
  32. offset four points by 'position'
  33.  
  34. drawline (midpoint tl,bl to midpoint tr,br)
  35. drawline (tl to bl)
  36. drawline (tr to br)
  37.  
  38. while x21 is negative, y13 will be positive
  39. while x21 is positive, y13 will be negative
  40. while y21 is positive, x13 will be positive
  41. while y21 is negative, x13 will be negative
  42.  
  43. x21 = x1 - x2
  44. y21 = y1 - y2
  45.  
  46. x13^2 + y13^2 = |1->3|^2
  47. x13^2 + y13^2 = (N/2)^2
  48.  
  49. x21*x13 + y21*y13 = 2->1 dot 1->3
  50. x21*x13 + y21*y13 = 0
  51.  
  52. x21*x13 = -y21*y13
  53. x13 = -(y21/x21)y13
  54.  
  55. x13 = -phi*y13
  56.  
  57. plug x13 into a
  58. phi^2*y13^2 + y13^2 = |1->3|^2
  59.  
  60. factor out y13
  61. y13^2 * (phi^2 + 1) =
  62.  
  63. plug in phi
  64. y13^2 * (y21^2/x21^2 + 1) =
  65.  
  66. multiply both sides by x21^2
  67. y13^2 * (y21^2 + x21^2) = |1->3|^2 * x21^2
  68.  
  69. plug in Pythagorean theorem of 2->1
  70. y13^2 * |2->1|^2 = |1->3|^2 * x21^2
  71.  
  72. take square root of both sides
  73. y13 * |2->1| = |1->3| * x21
  74.  
  75. divide both sides by the length of 1->2
  76. y13 = (|1->3|/|2->1|) *x21
  77.  
  78. lets call the ratio of 1->3 to 2->1 lengths psi
  79. y13 = psi * x21
  80.  
  81. check the signs
  82. when x21 is negative, y13 will be positive
  83. when x21 is positive, y13 will be negative
  84.  
  85. y13 = -psi * x21
  86.  
  87. plug y13 into a
  88. x13^2 + x13^2/phi^2 = |1->3|^2
  89.  
  90. factor out x13
  91. x13^2 * (1 + 1/phi^2) =
  92.  
  93. plug in phi
  94. x13^2 * (1 + x21^2/y21^2) =
  95.  
  96. multiply both sides by y21^2
  97. x13^2 * (y21^2 + x21^2) = |1->3|^2 * y21^2
  98.  
  99. plug in Pythagorean theorem of 2->1
  100. x13^2 * |2->1|^2 = |1->3|^2 * y21^2
  101.  
  102. take square root of both sides
  103. x13 * |2->1| = |1->3| * y21
  104.  
  105. divide both sides by the length of 2->1
  106. x13 = (|1->3|/|2->1|) *y21
  107.  
  108. lets call the ratio of |1->3| to |2->1| psi
  109. x13 = psi * y21
  110.  
  111. check the signs
  112. when y21 is negative, x13 will be negative
  113. when y21 is positive, x13 will be negative
  114.  
  115. x13 = psi * y21
  116.  
  117. x21 = x1 - x2
  118. y21 = y1 - y2
  119.  
  120. |2->1| = sqrt( x21^2 + y^21^2 )
  121. |1->3| = N/2
  122.  
  123. psi = |1->3|/|2->1|
  124.  
  125. y13 = -psi * x21
  126. x13 = psi * y21
Add Comment
Please, Sign In to add comment