From 2f48fe13cbd130db75fc45d6853b8839d94e2a61 Mon Sep 17 00:00:00 2001 From: David Runge Date: Sat, 2 Jan 2016 03:49:20 +0100 Subject: AutoVisual/IGPFamily.pde: Adding functions for to find intersections between lines (lineIntersections()), extend lines up until a given limit in one dimension (extendLine()) and to get the angle between two PVectors relative to the first one (angleBetweenTwoVectors()). --- AutoVisual/IGPFamily.pde | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/AutoVisual/IGPFamily.pde b/AutoVisual/IGPFamily.pde index 3108686..4d05542 100644 --- a/AutoVisual/IGPFamily.pde +++ b/AutoVisual/IGPFamily.pde @@ -164,6 +164,43 @@ class IGPFamily { translate(mid.x*(-1), mid.y*(-1)); } + PVector lineIntersection(PVector p1, PVector p2, PVector p3, PVector p4) { + PVector b = PVector.sub(p2, p1); + PVector d = PVector.sub(p4, p3); + + float b_dot_d_perp = b.x * d.y - b.y * d.x; + if (b_dot_d_perp == 0) { + return null; + } + + PVector c = PVector.sub(p3, p1); + float t = (c.x * d.y - c.y * d.x) / b_dot_d_perp; + if (t < 0 || t > 1) { return null; } + float u = (c.x * b.y - c.y * b.x) / b_dot_d_perp; + if (u < 0 || u > 1) { return null; } + + return new PVector(p1.x+t*b.x, p1.y+t*b.y); + } + + float angleBetweenTwoVectors (PVector p1, PVector p2) { + float angle = atan2(p1.y-p2.y, p1.x-p2.x); + if (angle < 0) { + angle+=TWO_PI; + } + return degrees(angle); + } + + PVector extendLine(PVector startPoint, PVector endPoint, float boundary, boolean vertical){ + float m,n; + m = (endPoint.y-startPoint.y)/(endPoint.x-startPoint.x); + n = endPoint.y-m*endPoint.x; + if (vertical){ + return new PVector((boundary-n)/m, boundary); + } else { + return new PVector(boundary, (m*boundary)+n); + } + } + void display() { createAlphaCircle(); } -- cgit v1.2.3-70-g09d2