| 50 | *-------------------------------------------------------------- |
| 51 | */ |
| 52 | |
| 53 | double |
| 54 | TkLineToPoint(end1Ptr, end2Ptr, pointPtr) |
| 55 | double end1Ptr[2]; /* Coordinates of first end-point of line. */ |
| 56 | double end2Ptr[2]; /* Coordinates of second end-point of line. */ |
| 57 | double pointPtr[2]; /* Points to coords for point. */ |
| 58 | { |
| 59 | double x, y; |
| 60 | |
| 61 | /* |
| 62 | * Compute the point on the line that is closest to the |
| 63 | * point. This must be done separately for vertical edges, |
| 64 | * horizontal edges, and other edges. |
| 65 | */ |
| 66 | |
| 67 | if (end1Ptr[0] == end2Ptr[0]) { |
| 68 | |
| 69 | /* |
| 70 | * Vertical edge. |
| 71 | */ |
| 72 | |
| 73 | x = end1Ptr[0]; |
| 74 | if (end1Ptr[1] >= end2Ptr[1]) { |
| 75 | y = MIN(end1Ptr[1], pointPtr[1]); |
| 76 | y = MAX(y, end2Ptr[1]); |
| 77 | } else { |
| 78 | y = MIN(end2Ptr[1], pointPtr[1]); |
| 79 | y = MAX(y, end1Ptr[1]); |
| 80 | } |
| 81 | } else if (end1Ptr[1] == end2Ptr[1]) { |
| 82 | |
| 83 | /* |
| 84 | * Horizontal edge. |
| 85 | */ |
| 86 | |
| 87 | y = end1Ptr[1]; |
| 88 | if (end1Ptr[0] >= end2Ptr[0]) { |
| 89 | x = MIN(end1Ptr[0], pointPtr[0]); |
| 90 | x = MAX(x, end2Ptr[0]); |
| 91 | } else { |
| 92 | x = MIN(end2Ptr[0], pointPtr[0]); |
| 93 | x = MAX(x, end1Ptr[0]); |
| 94 | } |
| 95 | } else { |
| 96 | double m1, b1, m2, b2; |
| 97 | |
| 98 | /* |
| 99 | * The edge is neither horizontal nor vertical. Convert the |
| 100 | * edge to a line equation of the form y = m1*x + b1. Then |
| 101 | * compute a line perpendicular to this edge but passing |
| 102 | * through the point, also in the form y = m2*x + b2. |
| 103 | */ |
| 104 | |
| 105 | m1 = (end2Ptr[1] - end1Ptr[1])/(end2Ptr[0] - end1Ptr[0]); |
| 106 | b1 = end1Ptr[1] - m1*end1Ptr[0]; |
| 107 | m2 = -1.0/m1; |
| 108 | b2 = pointPtr[1] - m2*pointPtr[0]; |
| 109 | x = (b2 - b1)/(m1 - m2); |