Graphviz  2.29.20120523.0446
lib/neatogen/geometry.c
Go to the documentation of this file.
00001 /* $Id$ $Revision$ */
00002 /* vim:set shiftwidth=4 ts=8: */
00003 
00004 /*************************************************************************
00005  * Copyright (c) 2011 AT&T Intellectual Property 
00006  * All rights reserved. This program and the accompanying materials
00007  * are made available under the terms of the Eclipse Public License v1.0
00008  * which accompanies this distribution, and is available at
00009  * http://www.eclipse.org/legal/epl-v10.html
00010  *
00011  * Contributors: See CVS logs. Details at http://www.graphviz.org/
00012  *************************************************************************/
00013 
00014 #include "geometry.h"
00015 #include <math.h>
00016 
00017 
00018 Point origin = { 0, 0 };
00019 
00020 double xmin, xmax, ymin, ymax;  /* min and max x and y values of sites */
00021 double deltax,                  /* xmax - xmin */
00022  deltay;                        /* ymax - ymin */
00023 
00024 int nsites;
00025 int sqrt_nsites;
00026 
00027 void geominit()
00028 {
00029     double sn;
00030 
00031     sn = nsites + 4;
00032     sqrt_nsites = (int) sqrt(sn);
00033     /* deltay = ymax - ymin; */
00034     /* deltax = xmax - xmin; */
00035 }
00036 
00037 double dist_2(Point * pp, Point * qp)
00038 {
00039     double dx = pp->x - qp->x;
00040     double dy = pp->y - qp->y;
00041 
00042     return (dx * dx + dy * dy);
00043 }
00044 
00045 void subpt(Point * a, Point b, Point c)
00046 {
00047     a->x = b.x - c.x;
00048     a->y = b.y - c.y;
00049 }
00050 
00051 void addpt(Point * c, Point a, Point b)
00052 {
00053     c->x = a.x + b.x;
00054     c->y = a.y + b.y;
00055 }
00056 
00057 double area_2(Point a, Point b, Point c)
00058 {
00059     return ((a.y - b.y) * (c.x - b.x) - (c.y - b.y) * (a.x - b.x));
00060 }
00061 
00062 int leftOf(Point a, Point b, Point c)
00063 {
00064     return (area_2(a, b, c) > 0);
00065 }
00066 
00067 int intersection(Point a, Point b, Point c, Point d, Point * p)
00068 {
00069     double s, t;                /* The two parameters of the parametric eqns. */
00070     double denom;               /* Denominator of solutions. */
00071 
00072     denom =
00073         a.x * (d.y - c.y) +
00074         b.x * (c.y - d.y) + d.x * (b.y - a.y) + c.x * (a.y - b.y);
00075 
00076     /* If denom is zero, then the line segments are parallel. */
00077     /* In this case, return false even though the segments might overlap. */
00078     if (denom == 0.0)
00079         return 0;
00080 
00081     s = (a.x * (d.y - c.y) + c.x * (a.y - d.y) + d.x * (c.y - a.y)
00082         ) / denom;
00083     t = -(a.x * (c.y - b.y) + b.x * (a.y - c.y) + c.x * (b.y - a.y)
00084         ) / denom;
00085 
00086     p->x = a.x + s * (b.x - a.x);
00087     p->y = a.y + s * (b.y - a.y);
00088 
00089     if ((0.0 <= s) && (s <= 1.0) && (0.0 <= t) && (t <= 1.0))
00090         return 1;
00091     else
00092         return 0;
00093 }