1 /*****************************************************************************/
2 /* 8888888 88888888 88888888 */
5 /* 8 88888888 88888888 */
8 /* 888888 888888888 888888888 */
10 /* A Two-Dimensional General Purpose Semiconductor Simulator. */
13 /* Last update: May 29, 2006 */
17 /* NINT, No.69 P.O.Box, Xi'an City, China */
19 /*****************************************************************************/
26 /* ----------------------------------------------------------------------------
27 * CompAngle: This function computes the angle between three points given
28 * by coordinates (x1,y1), (x2,y2) and (x3,y3). The angle is measured from
29 * vector 12 to 32.clock order
31 double CompAngle(double x1
, double y1
,double x2
, double y2
,double x3
, double y3
)
33 double xl
,yl
; /* coordinates of vector 12 */
34 double xr
,yr
; /* coordinates of vector 32 */
35 double r
; /* temporary number */
36 double angle
; /* angle between vectors 12 & 32 */
37 double PI
=3.14159265358979323846;
38 xl
= x1
- x2
; yl
= y1
- y2
;
39 xr
= x3
- x2
; yr
= y3
- y2
;
41 r
= (xl
*xr
+yl
*yr
)/(sqrt(xl
*xl
+yl
*yl
)*sqrt(xr
*xr
+yr
*yr
));
44 else if (r
< -1.0) r
= -1.0;
52 /* ----------------------------------------------------------------------------
53 * CircleCenter: This function determines the center of the circle that
54 * passes through points (x1,y1), (x2,y2) and (x3,y3). If the points are
55 * coplaner then 1 is returned otherwise 0 is
58 int CircleCenter(double x1
, double y1
, double x2
, double y2
,double x3
, double y3
,
59 double &xc
, double &yc
)
61 double xl
,yl
; /* coordinates of vector 12 */
62 double xr
,yr
; /* coordinates of vector 32 */
63 double rl
,rr
; /* ??? */
64 double det
; /* determinent of coordinate matrix */
66 xl
= x1
- x2
; yl
= y1
- y2
;
67 xr
= x3
- x2
; yr
= y3
- y2
;
69 det
= xr
* yl
- xl
* yr
;
76 rl
= (xl
* (x1
+ x2
) + yl
* (y1
+ y2
)) / det
;
77 rr
= (xr
* (x3
+ x2
) + yr
* (y3
+ y2
)) / det
;
79 xc
= -0.5 * (rl
* yr
- rr
* yl
);
80 yc
= -0.5 * (rr
* xl
- rl
* xr
);
85 /* ----------------------------------------------------------------------------
86 * MirrorPoint: This function computes the MirrorPoint(xg,yg) of (x,y) with the
87 * line determined by (x1,y1) and (x2,y2).
89 void MirrorPoint(double x
,double y
,double x1
,double y1
,double x2
,double y2
,double &xg
,double &yg
)
91 double r
,ang1
,ang2
,angle
;
94 double r1
= sqrt((x1
-x
)*(x1
-x
)+(y1
-y
)*(y1
-y
));
95 double r2
= sqrt((x2
-x
)*(x2
-x
)+(y2
-y
)*(y2
-y
));
98 r
= sqrt((x1
-x2
)*(x1
-x2
)+(y1
-y2
)*(y1
-y2
));
99 ang1
= CompAngle(x2
,y2
,x1
,y1
,x
,y
);
100 ang2
= CompAngle(x2
,y2
,x1
,y1
,x1
+r
,y1
);
102 xg
= x1
+ r1
*cos(angle
);
103 yg
= y1
+ r1
*sin(angle
);
107 r
= sqrt((x1
-x2
)*(x1
-x2
)+(y1
-y2
)*(y1
-y2
));
108 ang1
= CompAngle(x1
,y1
,x2
,y2
,x
,y
);
109 ang2
= CompAngle(x1
,y1
,x2
,y2
,x2
+r
,y2
);
111 xg
= x2
+ r2
*cos(angle
);
112 yg
= y2
+ r2
*sin(angle
);
118 /* ----------------------------------------------------------------------------
119 * DistancePointLine: This function computes the distance between point (x,y) and
120 * line determined by (x1,y1) and (x2,y2).
122 double DistancePointLine(double x1
, double y1
,double x2
, double y2
,double x
, double y
)
124 double LineMag
= Distance(x1
,y1
,x2
,y2
);
125 double U
= (((x
-x1
)*(x2
-x1
))+((y
-y1
)*(y2
-y1
))) / (LineMag
*LineMag
);
127 double IntersectionX
= x1
+ U
*(x2
-x1
);
128 double IntersectionY
= y1
+ U
*(y2
-y1
);
130 return Distance( x
, y
, IntersectionX
, IntersectionY
);
131 }/* DistancePointLine */
134 /* ----------------------------------------------------------------------------
135 * Distance: This function computes the distance between (x1,y1) and (x2,y2)
136 * line determined by (x1,y1) and (x2,y2).
138 double Distance(double x1
, double y1
,double x2
,double y2
)
140 if(x1
==x2
&& y1
==y2
) return 0;
141 return sqrt((x1
-x2
)*(x1
-x2
) +(y1
-y2
)*(y1
-y2
));
145 /* ----------------------------------------------------------------------------
146 * TriArea: This function computes the area of a triangle determined by (x1,y1)
147 * (x2,y2) and (x3,y3).
149 double TriArea(double x1
, double y1
,double x2
, double y2
,double x3
, double y3
)
151 double d1
,d2
,d3
,d
,r
,xc
,yc
;
152 d1
=Distance(x1
,y1
,x2
,y2
);
153 d2
=Distance(x1
,y1
,x3
,y3
);
154 d3
=Distance(x2
,y2
,x3
,y3
);
156 return sqrt(d
*(d
-d1
)*(d
-d2
)*(d
-d3
));
160 /* ----------------------------------------------------------------------------
161 * TriAngle: This function compute the max angle of a triangle
164 double MaxTriAngle(double x1
, double y1
, double x2
, double y2
,double x3
, double y3
)
167 a
=Distance(x1
,y1
,x2
,y2
);
168 b
=Distance(x1
,y1
,x3
,y3
);
169 c
=Distance(x2
,y2
,x3
,y3
);
170 if(a
>b
&&a
>c
) return acos((b
*b
+c
*c
-a
*a
)/(2*b
*c
));
171 if(b
>a
&&b
>c
) return acos((a
*a
+c
*c
-b
*b
)/(2*a
*c
));
172 if(c
>b
&&c
>a
) return acos((a
*a
+b
*b
-c
*c
)/(2*a
*b
));
173 return 0; //prevent warning of complier
177 /* ----------------------------------------------------------------------------
178 * RaySegmentIntersectTest: This function do 2D Ray-Segment Intersection test
181 IntersectResult
RaySegmentIntersectTest(double begin_x
, double begin_y
, double ex
, double ey
, double x1
,double y1
,double x2
,double y2
)
183 double end_x
= begin_x
+ ex
/sqrt(ex
*ex
+ey
*ey
+1e-20);
184 double end_y
= begin_y
+ ey
/sqrt(ex
*ex
+ey
*ey
+1e-20);
185 double denom
= ((y2
- y1
)*(end_x
- begin_x
)) -((x2
- x1
)*(end_y
- begin_y
));
186 double nume_a
= ((x2
- x1
)*(begin_y
- y1
)) - ((y2
- y1
)*(begin_x
- x1
));
187 double nume_b
= ((end_x
- begin_x
)*(begin_y
- y1
)) - ((end_y
- begin_y
)*(begin_x
- x1
));
190 if(nume_a
== 0.0 && nume_b
== 0.0)
197 double ua
= nume_a
/ denom
;
198 double ub
= nume_b
/ denom
;
200 if(ub
>= 0.0 && ub
<= 1.0)
202 // Get the intersection point.
203 double intersection_x
= begin_x
+ ua
*(end_x
- begin_x
);
204 double intersection_y
= begin_y
+ ua
*(end_y
- begin_y
);
205 return INTERESECTING
;
208 return NOT_INTERESECTING
;