00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "geometry.h"
00018 #include <math.h>
00019
00020
00021 Point origin = { 0, 0 };
00022
00023 double xmin, xmax, ymin, ymax;
00024 double deltax,
00025 deltay;
00026
00027 int nsites;
00028 int sqrt_nsites;
00029
00030 void geominit()
00031 {
00032 double sn;
00033
00034 sn = nsites + 4;
00035 sqrt_nsites = (int) sqrt(sn);
00036
00037
00038 }
00039
00040 double dist_2(Point * pp, Point * qp)
00041 {
00042 double dx = pp->x - qp->x;
00043 double dy = pp->y - qp->y;
00044
00045 return (dx * dx + dy * dy);
00046 }
00047
00048 void subPt(Point * a, Point b, Point c)
00049 {
00050 a->x = b.x - c.x;
00051 a->y = b.y - c.y;
00052 }
00053
00054 void addPt(Point * c, Point a, Point b)
00055 {
00056 c->x = a.x + b.x;
00057 c->y = a.y + b.y;
00058 }
00059
00060 double area_2(Point a, Point b, Point c)
00061 {
00062 return ((a.y - b.y) * (c.x - b.x) - (c.y - b.y) * (a.x - b.x));
00063 }
00064
00065 int leftOf(Point a, Point b, Point c)
00066 {
00067 return (area_2(a, b, c) > 0);
00068 }
00069
00070 int intersection(Point a, Point b, Point c, Point d, Point * p)
00071 {
00072 double s, t;
00073 double denom;
00074
00075 denom =
00076 a.x * (d.y - c.y) +
00077 b.x * (c.y - d.y) + d.x * (b.y - a.y) + c.x * (a.y - b.y);
00078
00079
00080
00081 if (denom == 0.0)
00082 return 0;
00083
00084 s = (a.x * (d.y - c.y) + c.x * (a.y - d.y) + d.x * (c.y - a.y)
00085 ) / denom;
00086 t = -(a.x * (c.y - b.y) + b.x * (a.y - c.y) + c.x * (b.y - a.y)
00087 ) / denom;
00088
00089 p->x = a.x + s * (b.x - a.x);
00090 p->y = a.y + s * (b.y - a.y);
00091
00092 if ((0.0 <= s) && (s <= 1.0) && (0.0 <= t) && (t <= 1.0))
00093 return 1;
00094 else
00095 return 0;
00096 }