/ IntersectionPointLinepointObj *p, pointObj *a, pointObj *b) */ / Retunrs a point object corresponding to the intersection of */ point p and a line formed of 2 points : a and b. */ / Alorith base on : */ http://www.faqs.org/faqs/graphics/algorithms-faq/ */ / Subject 1.02:How do I find the distance from a point to a
| 1117 | /* */ |
| 1118 | /************************************************************************/ |
| 1119 | pointObj *msIntersectionPointLine(pointObj *p, pointObj *a, pointObj *b) |
| 1120 | { |
| 1121 | double r = 0; |
| 1122 | double L = 0; |
| 1123 | pointObj *result = NULL; |
| 1124 | |
| 1125 | if (p && a && b) |
| 1126 | { |
| 1127 | L = sqrt(((b->x - a->x)*(b->x - a->x)) + |
| 1128 | ((b->y - a->y)*(b->y - a->y))); |
| 1129 | |
| 1130 | if (L != 0) |
| 1131 | r = ((p->x - a->x)*(b->x - a->x) + (p->y - a->y)*(b->y - a->y))/(L*L); |
| 1132 | else |
| 1133 | r = 0; |
| 1134 | |
| 1135 | result = (pointObj *)msSmallMalloc(sizeof(pointObj)); |
| 1136 | /* -------------------------------------------------------------------- */ |
| 1137 | /* We want to make sure that the point returned is on the line */ |
| 1138 | /* */ |
| 1139 | /* r=0 P = A */ |
| 1140 | /* r=1 P = B */ |
| 1141 | /* r<0 P is on the backward extension of AB */ |
| 1142 | /* r>1 P is on the forward extension of AB */ |
| 1143 | /* 0<r<1 P is interior to AB */ |
| 1144 | /* -------------------------------------------------------------------- */ |
| 1145 | if (r < 0) |
| 1146 | { |
| 1147 | result->x = a->x; |
| 1148 | result->y = a->y; |
| 1149 | } |
| 1150 | else if (r > 1) |
| 1151 | { |
| 1152 | result->x = b->x; |
| 1153 | result->y = b->y; |
| 1154 | } |
| 1155 | else |
| 1156 | { |
| 1157 | result->x = a->x + r*(b->x - a->x); |
| 1158 | result->y = a->y + r*(b->y - a->y); |
| 1159 | } |
| 1160 | #ifdef USE_POINT_Z_M |
| 1161 | result->m = 0; |
| 1162 | #endif |
| 1163 | } |
| 1164 | |
| 1165 | return result; |
| 1166 | } |
| 1167 | |
| 1168 | |
| 1169 | /************************************************************************/ |
no test coverage detected