public static void main(String[] args) { Point3D p1 = new Point3D(0.0, 0.0, 0.0); Point3D p2 = new Point3D(4.0, 4.0, 4.0); Line3D l1 = new Line3D(p1, p2); Point3D p3 = new Point3D(0.0, 4.0, 4.0); Point3D p4 = new Point3D(4.0, 0.0, 0.0); Line3D l2 = new Line3D(p3, p4); System.out.println("p1: " + p1); System.out.println("p2: " + p2); System.out.println("l1: " + l1); System.out.println("l1.intersection(p2): " + l1.intersection(p2)); System.out.println("p2.intersection(l1): " + p2.intersection(l1)); System.out.println("l2: " + l2); System.out.println("l2.intersection(l1): " + l2.intersection(l1)); System.out.println("l1.intersection(l2): " + l1.intersection(l2)); System.out.println("Object3D c1 = l1.union(l2)"); Object3D c1 = l1.union(l2); System.out.println("c1: \n" + c1.toString()); System.out.println("p1.union(p4): \n" + p1.union(p4)); System.out.println("c1.intersection(p1.union(p4))"); System.out.println(c1.intersection(p1.union(p4))); System.out.println("c1.union(p3):\n" + c1.union(p3)); }
/** @see Object3D#intersection(Object3D) */ public Object3D intersection(Object3D space) { double cutxyX = 0.0, cutxzX = 0.0; if (space instanceof Point3D) { Point3D other = (Point3D) space; if ((this.gradeY * other.x + this.offsetY == other.y) && (this.gradeZ * other.x + this.offsetZ == other.z)) return (Object3D) other.clone(); else return Object3DVoid; } if (space instanceof Line3D) { Line3D test = (Line3D) space; // Exclusion of parallel lines which never will intersect each others. if (test.gradeY == this.gradeY) { if (this.offsetY != test.offsetY) return Object3DVoid; // parallel lines in x-y area (they will never touch in this // dimension) } if (test.gradeZ == this.gradeZ) { if (this.offsetZ != test.offsetZ) return Object3DVoid; // parallel lines in x-z area (they will never touch in this // dimension) } // mathematical search for the intersection point: // has to cut in x-y dimension: cutxyX = (this.offsetY - test.offsetY) / (test.gradeY - this.gradeY); cutxzX = (this.offsetZ - test.offsetZ) / (test.gradeZ - this.gradeZ); if (cutxyX == 0.0 / 0.0) { // Parallel lines are caught above. // In xy-dimension the lines are matching. (see calculation of cutxyX) // Now there are two possibilities: // - The lines are also matching in dimension xz (cutxzX is NaN too): If the x-ranges of // both lines are overlapping "intersection" is true. // - The lines have to cross: Check wether cutxzX is in x-range of both lines. if (cutxzX == 0.0 / 0.0) { if (!((this.end.x < test.start.x) // start.x is smaller end.x! (constructor) || (this.start.x > test.end.x))) { Point3D startx = (this.start.x > test.start.x) ? this.start : test.start; Point3D endx = (this.end.x < test.end.x) ? this.end : test.end; return new Line3D(startx, endx); } else return Object3DVoid; } else { // on zx-area it's not matching, so we have to check wether the intersection of both // lines of the zx-area is within the x-bounds of both lines. if ((cutxzX >= this.start.x) && (cutxzX <= this.end.x) && (cutxzX >= test.start.x) && (cutxzX <= test.end.x)) return new Point3D( cutxzX, this.gradeY * cutxzX + this.offsetY, this.gradeZ * cutxzX + this.offsetZ); else return Object3DVoid; } } // The lines have an intersection in xy-dimension else { // this is the other way round: // either in xz - dimension the lines are on the same way: if (cutxzX == 0.0 / 0.0) { // in zx - area the lines are matching in grade and offset. // Then we will only have to check, wether the crossing of the lines in // xy-dimension is within the x-bounds of both lines. if ((cutxyX >= this.start.x) && (cutxyX <= this.end.x) && (cutxyX >= test.start.x) && (cutxyX <= test.end.x)) return new Point3D( cutxyX, cutxyX * this.gradeY + this.offsetY, cutxyX * this.gradeZ + this.offsetZ); else return Object3DVoid; } // The lines are crossing in xy and xz dimension: // only if both x values cutxyX and cutxzX are equal and within the // x-ranges of both lines this is an intersection: else { if ((cutxyX == cutxzX) && ((cutxyX >= this.start.x) && (cutxyX <= this.end.x)) && ((cutxyX >= test.start.x) && (cutxyX <= test.end.x))) { return new Point3D( cutxyX, cutxyX * this.gradeY + this.offsetY, cutxyX * this.gradeZ + this.offsetZ); } else return Object3DVoid; } } } return space.intersection(this); }