11 |
* @param p the projection center |
* @param p the projection center |
12 |
* @param ctr The center of the circle |
* @param ctr The center of the circle |
13 |
* @param rad The radius of the circle |
* @param rad The radius of the circle |
14 |
|
* @param ansdir -1 or 1 : 1 = take further-away answer |
15 |
|
* @param success Optional, the pointer to a boolean set if the projection was possible. |
16 |
* @return The projected point, or if not possible, the original pt. |
* @return The projected point, or if not possible, the original pt. |
17 |
*/ |
*/ |
18 |
inline Vec project2circle(Vec pt, Vec p, Vec ctr, float rad) { |
inline Vec project2circle(Vec pt, Vec p, Vec ctr, float rad, int ansdir = 1, |
19 |
|
bool *success = 0) { |
20 |
Vec ao = pt - ctr; |
Vec ao = pt - ctr; |
21 |
Vec ap = pt - p; |
Vec ap = pt - p; |
22 |
|
|
28 |
// determinant of the equation |
// determinant of the equation |
29 |
float det = b*b - 4*a*c; |
float det = b*b - 4*a*c; |
30 |
|
|
31 |
float ans = (det > 0 ? (-b + sqrt(det)) / (2*a) : 0); |
float ans = (det > 0 ? (-b + ansdir * sqrt(det)) / (2*a) : 0); |
32 |
|
if(success) *success = (det > 0); |
33 |
|
|
34 |
return pt + ans * ap; |
return pt + ans * ap; |
35 |
|
|
36 |
} |
} |
37 |
|
|
38 |
|
/** Given a point and a normal there and a circle, |
39 |
|
* find the circle that passes through the point |
40 |
|
* with the given normal (normal pointing to *center* |
41 |
|
* of circle) and is tangential to the circle. |
42 |
|
* Norm must be normalized. |
43 |
|
*/ |
44 |
|
inline Vec circle__point_norm_circle( |
45 |
|
Vec pt, |
46 |
|
Vec norm, |
47 |
|
Vec ctr, |
48 |
|
float r, |
49 |
|
bool *success = 0) { |
50 |
|
Vec q = pt - r * norm; |
51 |
|
Vec c = lerp(ctr, q, .5); |
52 |
|
using namespace Vec23; |
53 |
|
HL lc = HPt(c).line(HPt(c + (q - ctr).cw90())); |
54 |
|
HL ln = HPt(q).line(pt); |
55 |
|
HPt it = lc.intersection(ln); |
56 |
|
if(it.finite()) { |
57 |
|
if(success) |
58 |
|
*success = 1; |
59 |
|
return (Vec)it; |
60 |
|
} else { |
61 |
|
if(success) |
62 |
|
*success = 0; |
63 |
|
return pt; |
64 |
|
} |
65 |
|
} |
66 |
|
|
67 |
|
|
68 |
} |
} |
69 |
} |
} |