1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// hdGeometry.cpp - Utility Geometric Functions Shared between classes
//
//////////////////////////////////////////////////////////////////////////
#include "pgAdmin3.h"
// wxWindows headers
#include <wx/wx.h>
// App headers
#include "hotdraw/utilities/hdGeometry.h"
#include "hotdraw/utilities/hdMultiPosRect.h"
#include "hotdraw/utilities/hdPoint.h"
//
// Warning when using it: typecasting to avoid miscalculations functions need double values not int
//
bool hdGeometry::lineContainsPoint(double x1, double y1, double x2, double y2, double px, double py)
{
hdPoint p = hdPoint(x1, y1);
hdRect r = hdRect(p);
r.add(x2, y2);
r.Inflate(2, 2);
if(!r.Contains(px, py))
{
return false;
}
double a, b, x, y;
double val1, val2;
val1 = px - x1;
if( x1 == x2 )
{
return (ddabs(val1) < 3);
}
val2 = py - y1;
if( y1 == y2 )
{
return (ddabs(val2) < 3);
}
a = (y1 - y2) / (x1 - x2);
b = y1 - a * x1;
x = (py - b) / a;
y = a * px + b;
val1 = x - px;
val2 = y - py;
bool out = (min( ddabs(val1), ddabs(val2)) < 4);
return out;
}
int hdGeometry::min(double a, double b)
{
return(a <= b) ? a : b;
}
int hdGeometry::max(double a, double b)
{
return(a >= b) ? a : b;
}
int hdGeometry::min(int a, int b)
{
return(a <= b) ? a : b;
}
int hdGeometry::max(int a, int b)
{
return(a >= b) ? a : b;
}
//Gets the angle of a point relative to a rectangle.
double hdGeometry::angleFromPoint(int posIdx, hdMultiPosRect r, hdPoint point)
{
return angleFromPoint(r.gethdRect(posIdx), point);
}
double hdGeometry::angleFromPoint(hdRect r, hdPoint point)
{
double rx = point.x - r.center().x;
double ry = point.y - r.center().y;
return atan2 (ry * r.width, rx * r.height);
}
hdPoint hdGeometry::edgePointFromAngle(int posIdx, hdMultiPosRect r, double angle)
{
return edgePointFromAngle(r.gethdRect(posIdx), angle);
}
//Gets the point on a rectangle that corresponds to the given angle.
hdPoint hdGeometry::edgePointFromAngle(hdRect r, double angle)
{
static hdPoint locationPoint; //Hack to allow bug in linux & ddabs
double sinv = sin(angle);
double cosv = cos(angle);
double e = 0.0001;
double x = 0.0;
double y = 0.0;
double width = r.width;
double height = r.height;
if( ddabs(sinv) > e )
{
x = (1.0 + cosv / ddabs (sinv)) / 2.0 * width;
x = range(0.0, width, x);
}
else if ( cosv >= 0.0 )
{
x = width;
}
if ( ddabs(cosv) > e )
{
y = (1.0 + sinv / ddabs (cosv)) / 2.0 * height;
y = range (0.0, height, y);
}
else if ( sinv >= 0.0 )
{
y = height;
}
int xx = r.x + x;
int yy = r.y + y;
locationPoint = hdPoint(xx, yy);
return locationPoint;
}
double hdGeometry::range(double min, double max, double num)
{
return num < min ? min : (num > max ? max : num);
}
double hdGeometry::ddabs(double value)
{
return value < 0 ? (value * -1) : value;
}
int hdGeometry::ddabs(int value)
{
return value < 0 ? (value * -1) : value;
}
double hdGeometry::lineSize (hdPoint p1, hdPoint p2)
{
int w = p1.x - p2.x;
int h = p1.y - p2.y;
double perimeter = w * w + h * h;
return sqrt (perimeter);
}
// source: http://vision.dai.ed.ac.uk/andrewfg/c-g-a-faq.html
// Standard line intersection algorithm, Return true intersection if it exists, else false.
bool hdGeometry::intersection(hdPoint p1, hdPoint p2, hdPoint p3, hdPoint p4)
{
// Store the values for fast access and easy
// equations-to-code conversion
float x1 = p1.x, x2 = p2.x, x3 = p3.x, x4 = p4.x;
float y1 = p1.y, y2 = p2.y, y3 = p3.y, y4 = p4.y;
float d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
// If d is zero, there is no intersection
if (d == 0) return false;
// Get the x and y
float pre = (x1 * y2 - y1 * x2), post = (x3 * y4 - y3 * x4);
// point of intersection
float x = ( pre * (x3 - x4) - (x1 - x2) * post ) / d;
float y = ( pre * (y3 - y4) - (y1 - y2) * post ) / d;
// Check if the x and y coordinates are within both lines
if ( x < min(x1, x2) || x > max(x1, x2) || x < min(x3, x4) || x > max(x3, x4) )
return false;
if ( y < min(y1, y2) || y > max(y1, y2) || y < min(y3, y4) || y > max(y3, y4) )
return false;
return true;
}
|