@Test public void intersectionPoint() throws Exception { aStart = GeometryFactory.createVector(0,0); aDirection = GeometryFactory.createVector(1,0); a = GeometryFactory.createRay2D(aStart, aDirection); bStart = GeometryFactory.createVector(0,1); bDirection = GeometryFactory.createVector(1,0); b = GeometryFactory.createRay2D(bStart, bDirection); assertNull(a.intersectionPoint(b)); aStart = GeometryFactory.createVector(0,0); aDirection = GeometryFactory.createVector(1,0); a = GeometryFactory.createRay2D(aStart, aDirection); b = GeometryFactory.createRay2D(aStart, aDirection); assertNull(a.intersectionPoint(b)); aStart = GeometryFactory.createVector(1,1); aDirection = GeometryFactory.createVector(1,0); a = GeometryFactory.createRay2D(aStart, aDirection); bStart = GeometryFactory.createVector(2,1); bDirection = GeometryFactory.createVector(1,0); b = GeometryFactory.createRay2D(bStart, bDirection); assertNull(a.intersectionPoint(b)); aStart = GeometryFactory.createVector(5,0); aDirection = GeometryFactory.createVector(-1,0); bStart = GeometryFactory.createVector(0,5); bDirection = GeometryFactory.createVector(0,-1); a = GeometryFactory.createRay2D(aStart, aDirection); b = GeometryFactory.createRay2D(bStart, bDirection); intersection = a.intersectionPoint(b); Assert.assertEquals(0, intersection.getXComponent(), PRECISION); Assert.assertEquals(0, intersection.getXComponent(), PRECISION); aStart = GeometryFactory.createVector(2,0); aDirection = GeometryFactory.createVector(1,2); bStart = GeometryFactory.createVector(0,2); bDirection = GeometryFactory.createVector(2,1); a = GeometryFactory.createRay2D(aStart, aDirection); b = GeometryFactory.createRay2D(bStart, bDirection); intersection = a.intersectionPoint(b); Assert.assertEquals(4, intersection.getXComponent(), PRECISION); Assert.assertEquals(4, intersection.getXComponent(), PRECISION); }
public Vector2D intersectionPoint(Ray2D other) { double dx = other.getStart().getXComponent() - this.getStart().getXComponent(); double dy = other.getStart().getYComponent() - this.getStart().getYComponent(); double det = other.getDirection().getXComponent() * this.getDirection().getYComponent() - other.getDirection().getYComponent() * this.getDirection().getXComponent(); if(det == 0) { return null; } double u = (dy * other.getDirection().getXComponent() - dx * other.getDirection().getYComponent()) / det; double v = (dy * this.getDirection().getXComponent() - dx * this.getDirection().getYComponent()) / det; if(u >= 0 && v >= 0) { return this.getStart().sum(this.getDirection().multiply(u)); } else { return null; } }
Ray2D { public Vector2D intersectionPoint(Ray2D other) { double dx = other.getStart().getXComponent() - this.getStart().getXComponent(); double dy = other.getStart().getYComponent() - this.getStart().getYComponent(); double det = other.getDirection().getXComponent() * this.getDirection().getYComponent() - other.getDirection().getYComponent() * this.getDirection().getXComponent(); if(det == 0) { return null; } double u = (dy * other.getDirection().getXComponent() - dx * other.getDirection().getYComponent()) / det; double v = (dy * this.getDirection().getXComponent() - dx * this.getDirection().getYComponent()) / det; if(u >= 0 && v >= 0) { return this.getStart().sum(this.getDirection().multiply(u)); } else { return null; } } }
Ray2D { public Vector2D intersectionPoint(Ray2D other) { double dx = other.getStart().getXComponent() - this.getStart().getXComponent(); double dy = other.getStart().getYComponent() - this.getStart().getYComponent(); double det = other.getDirection().getXComponent() * this.getDirection().getYComponent() - other.getDirection().getYComponent() * this.getDirection().getXComponent(); if(det == 0) { return null; } double u = (dy * other.getDirection().getXComponent() - dx * other.getDirection().getYComponent()) / det; double v = (dy * this.getDirection().getXComponent() - dx * this.getDirection().getYComponent()) / det; if(u >= 0 && v >= 0) { return this.getStart().sum(this.getDirection().multiply(u)); } else { return null; } } Ray2D(Vector2D start, Vector2D direction); }
Ray2D { public Vector2D intersectionPoint(Ray2D other) { double dx = other.getStart().getXComponent() - this.getStart().getXComponent(); double dy = other.getStart().getYComponent() - this.getStart().getYComponent(); double det = other.getDirection().getXComponent() * this.getDirection().getYComponent() - other.getDirection().getYComponent() * this.getDirection().getXComponent(); if(det == 0) { return null; } double u = (dy * other.getDirection().getXComponent() - dx * other.getDirection().getYComponent()) / det; double v = (dy * this.getDirection().getXComponent() - dx * this.getDirection().getYComponent()) / det; if(u >= 0 && v >= 0) { return this.getStart().sum(this.getDirection().multiply(u)); } else { return null; } } Ray2D(Vector2D start, Vector2D direction); Vector2D getStart(); void setStart(Vector2D start); Vector2D getDirection(); void setDirection(Vector2D direction); Vector2D intersectionPoint(Ray2D other); Ray2D intersectionRay(Ray2D other); Segment2D intersectionSegment(Ray2D other); boolean contains(Vector2D point); boolean isParallel(Ray2D other); boolean equals(Ray2D other); String toString(); }
Ray2D { public Vector2D intersectionPoint(Ray2D other) { double dx = other.getStart().getXComponent() - this.getStart().getXComponent(); double dy = other.getStart().getYComponent() - this.getStart().getYComponent(); double det = other.getDirection().getXComponent() * this.getDirection().getYComponent() - other.getDirection().getYComponent() * this.getDirection().getXComponent(); if(det == 0) { return null; } double u = (dy * other.getDirection().getXComponent() - dx * other.getDirection().getYComponent()) / det; double v = (dy * this.getDirection().getXComponent() - dx * this.getDirection().getYComponent()) / det; if(u >= 0 && v >= 0) { return this.getStart().sum(this.getDirection().multiply(u)); } else { return null; } } Ray2D(Vector2D start, Vector2D direction); Vector2D getStart(); void setStart(Vector2D start); Vector2D getDirection(); void setDirection(Vector2D direction); Vector2D intersectionPoint(Ray2D other); Ray2D intersectionRay(Ray2D other); Segment2D intersectionSegment(Ray2D other); boolean contains(Vector2D point); boolean isParallel(Ray2D other); boolean equals(Ray2D other); String toString();  }