I have the code for circle-circle intersection. But I need to expand it to 3-D. Could you please help me to write the functions?
static class Point{
double x, y, z;
int dimension;
Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
dimension = 3;
}
Point sub(Point p2) {
return new Point(x - p2.x, y - p2.y, z - p2.z);
}
Point add(Point p2) {
return new Point(x + p2.x, y + p2.y, z + p2.z);
}
double distance(Point p2) {
return Math.sqrt((x - p2.x)*(x - p2.x) + (y - p2.y)*(y - p2.y) + (z - p2.z)*(z - p2.z));
}
Point normal() {
double length = Math.sqrt(x*x + y*y + z*z);
return new Point(x/length, y/length, z/length);
}
Point scale(double s) {
return new Point(x*s, y*s, z*s);
}
double[] array()
{
return new double[]{x,y,z};
}
}
static class Circle {
double x, y, r, left;
Circle(double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
left = x - r;
}
Circle(double[] c, double r) {
this(c[0], c[1], r);
}
Circle(Point c, double r)
{
this(c.x, c.y, r);
}
Point[] intersections(Circle c)
{
Point P0 = new Point(x, y,0);
Point P1 = new Point(c.x, c.y,0);
double d, a, h;
d = P0.distance(P1);
a = (r*r - c.r*c.r + d*d)/(2*d);
h = Math.sqrt(r*r - a*a);
if(Double.isNaN(h))
return null;
Point P2 = P1.sub(P0).scale(a/d).add(P0);
double x3, y3, x4, y4;
x3 = P2.x + h*(P1.y - P0.y)/d;
y3 = P2.y - h*(P1.x - P0.x)/d;
x4 = P2.x - h*(P1.y - P0.y)/d;
y4 = P2.y + h*(P1.x - P0.x)/d;
return new Point[]{new Point(x3, y3, 0), new Point(x4, y4, 0)};
}
}
static class Sphere
{
double x,y,z,r,left;
Sphere(double x, double y, double z, double r)
{
this.x = x;
this.y = y;
this.z = z;
this.r = r;
left = x-r;
}
Circle intersection(Sphere s)
{
Point P0 = new Point(x, y, z);
Point P1 = new Point(s.x, s.y, s.z);
double d, a, h;
d = P0.distance(P1);
a = (r*r - s.r*s.r + d*d)/(2*d);
h = Math.sqrt(r*r - a*a);
if(Double.isNaN(h))
return null;
Point P2 = P1.sub(P0).scale(a/d).add(P0);
return new Circle(P2, h);
}
Point[] intersections(Circle c)
{
Point P0 = new Point(0,0,0);
Point P1 = new Point(0,0,0);
//...
return new Point[]{P0, P1};
}
}
I have checked this link and this link, but I could not understand the logic behind them and how to code them.
I tried ProGAL library for sphere-sphere-sphere intersection, but the resulting coordinates are rounded. I need precise results.
To be sure , what you need is how to intersect 3 spheres?
if so , your current data structure for circle is useless.
let say you want to intersect 3 spheres : s1 , s2 , s3, intersection of s1 and s2 will be a circle that you will intersect with s3 for final results, BUT keep it mind that the circle is not on XoY plane , it's center may be on any 3D coordinate and it will face a direction in the 3D space. to store information of such a circle you need , centerX , centerY , centerZ , radius and a 3d vector called normal.
class Circle3D
{
Point center;
double r;
Point normal;
...
}
after you completed this new class, you can use it to store information about Sphere-Sphere intersection. after that you must implement Sphere-Circle3D intersection:
Circle3D Intersect(Sphere s1 , Sphere s2)
{
double d = dist(s1.center , center)
double x = (d*d + s1.r*s1.r - s2.r*s2.r)/(2*d)
Point normal = normalize(s2.Center - s1.Center)
Point center = s1.center + x*normal;
double radius = sqrt(s1.r*s1.r - x*x)
return new Circle3D(center , radius , normal);
}
don't panic if math seems chaotic , read this Page
now it is timee ofr sphere-circle3D intersection, for that:
point[] Intersect(Sphere s , Circle3D c)
{
/*
first we check if sphere even intersects with plane of c
I assume you know how to implement some if these functions
*/
if(GetDistanceOfPointFromPlane(s.center , new Plane(c.center , normal))>s.radius)
return NULL;
/*
again we check possibility of avoiding math of intersection
*/
point dir = Normalize(s.center - c.center);
if(!DoesRayIntersectWithSphere(s , new Ray(c.center , c.radius*dir)))
return NULL;
/*
this is the ugly part of code that unfortunately is deep trig math.
you must describe sphere and circle equations in polar system , then
you must solve sphere = circle
I hope the link below be helpful
*/
}
here is the link mentioned in cemments above
http://mathworld.wolfram.com/SphericalCoordinates.html
Related
The user inputs 3 space separated coordinates that can make up a rectangle in the xy-plane. The algorithm returns what must be the 4th point to form a rectangle.
Example: "5 5", "5 7", and "7 5", newline separated, should return "7 7".
The below algorithm works for the provided test cases, but I am failing other cases, and I can't figure out why. Can anyone suggest a way to make my algorithm include all possible inputs - assuming that the 3 inputs provided do in fact form 3 corners of a rectangle?
import java.io.*;
public class cetvrta {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String point1 = reader.readLine(); // 5 5
String point2 = reader.readLine(); // 5 7
String point3 = reader.readLine(); // 7 5
String[] cord1 = point1.split(" "); // ["5","5"]
String[] cord2 = point2.split(" "); // ["5", "7"]
String[] cord3 = point3.split(" "); // ["7", "5"]
int x4 = 0;
int y4 = 0;
int x1 = Integer.parseInt(cord1[0]); // 5
int y1 = Integer.parseInt(cord1[1]); // 5
int x2 = Integer.parseInt(cord2[0]);
int y2 = Integer.parseInt(cord2[1]);
int x3 = Integer.parseInt(cord3[0]);
int y3 = Integer.parseInt(cord3[1]);
if (y1 == y2) {
if (x3 == x1) {
x4 = x2;
y4 = y3;
}
if (x3 == x2) {
x4 = x1;
y4 = y3;
}
}
if (y3 == y2) {
if (x2 == x3) {
x4 = x1;
y4 = y2;
}
if (x2 == x1) {
x4 = x3;
y4 = y2;
}
}
if (y1 == y3) {
if (x2 == x1) {
x4 = x3;
y4 = y2;
}
if (x2 == x3) {
x4 = x1;
y4 = y2;
}
}
System.out.println(x4 + " " + y4);
}
}
There is no hard and fast rule that "x-coordinates of 2 points of a rectangle has to match and so do the y-coordinates". Consider the image below for better understanding.
We can see that no two points have same x and y coordinates although there exists a perfect rectangle:
Fix:
I would recommend you to slightly change the algorithm as to proceed in the following way. Given the three points, find the point that isn't the corner(the one that does not pass through diagonal based out of other 2 points). From this point, calculate the slope to remaining points and assuming the 4th corner to be (x,y); draw out 2 locii. to satisfy slope1 * slope 2=-1. These 2 locii solved together will give the 4th point.
This is weird, rethink that:
if (y3 == y2) {
if (x2 == x3) { // <---
x4 = x1; // <---
y4 = y2; // <---
}
if (x2 == x1) {
x4 = x3;
y4 = y2; // <---
}
}
It would be better to use basic vector algebra to resolve this task:
calculate vectors between the three known points
define which point of the given three is a vertex of right angle (90°) - if any
this can be done using the fact that the scalar product of two perpendicular vectors is 0: v1.x * v2.x + v1.y * v2.0 == 0
find the fourth point by adding to the right angle vertex two vectors outgoing from this vertex to the other two known points.
Sample implementation could look like this:
// auxiliary classes
class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
Point add(Vector v) {
return new Point(this.x + v.x, this.y + v.y);
}
#Override
public String toString() {
return String.format("(%d, %d)", x, y);
}
}
class Vector {
int x, y;
Vector(int x, int y) {
this.x = x;
this.y = y;
}
Vector(Point p1, Point p2) {
this(p2.x - p1.x, p2.y - p1.y);
}
static boolean isRightAngle(Vector v1, Vector v2) {
return 0 == (v1.x * v2.x + v1.y * v2.y);
}
Vector add(Vector v) {
return new Vector(this.x + v.x, this.y + v.y);
}
}
Method to find the right angle vertex:
static int rightAngleVertexIndex(Point ... p) {
assert p.length == 3;
Vector v01 = new Vector(p[0], p[1]);
Vector v12 = new Vector(p[1], p[2]);
Vector v20 = new Vector(p[2], p[0]);
if (Vector.isRightAngle(v01, v12)) {
return 1;
} else if (Vector.isRightAngle(v12, v20)) {
return 2;
} else if (Vector.isRightAngle(v20, v01)) {
return 0;
} else {
return -1;
}
}
Method to find the 4th point of rectangle (return null if no rectangle is possible):
static Point findFourthVertex(Point ... points) {
assert points.length == 3;
final int[][] otherVertices = {
{1, 2},
{0, 2},
{0, 1},
};
Point result = null;
int rightAngleIx = rightAngleVertexIndex(points);
if (rightAngleIx != -1) {
Point rightAngle = points[rightAngleIx];
Point p1 = points[otherVertices[rightAngleIx][0]];
Point p2 = points[otherVertices[rightAngleIx][1]];
result = rightAngle.add(new Vector(rightAngle, p1).add(new Vector(rightAngle, p2)));
System.out.println("The fourth vertex of the rectangle: " + result);
} else {
System.out.println("No right angle found between any of the points " + Arrays.toString(points));
}
return result;
}
Test:
findFourthVertex(new Point(1, 1), new Point(5, 1), new Point(1, 4));
findFourthVertex(new Point(-1, -1), new Point(5, 0), new Point(6, 5));
Output:
The fourth vertex of the rectangle: (5, 4)
No right angle found between any of the points [(-1, -1), (5, 0), (6, 5)]
I am working on a desktop app that will help students with algebra. I have a class called Vector2 that just stores two variables, x and y. I am working on a method that will return the endpoint of a line given an endpoint and a midpoint. Just to show how I have been doing this here is a method
public static Vector2 midpoint(double xa, double ya, double xb, double yb){
Vector2 v = new Vector2(0, 0);
v.x = (xa + xb) / 2;
v.y = (ya + yb) / 2;
return v;
}
Given this how would I make a method that will give me the other endpoint?
Example: if the given endpoint is (-3, -5) and the given midpoint is (-6, -2) then the output should be (-9, 1)
EDIT FOR NEW PPL: I have the answer, it is pretty simple. Here is the final method
public static Vector2 otherEndpoint(double endPointX, double EndPointY, double midPointX, double midPointY){
Vector2 v = new Vector2(0,0);
v.x = (endPointX + midPointX) / 2;
v.y = (EndPointY + midPointY) / 2;
v.multiply(2);
return v;
}
v.multiply() is a method that I made that will multiply each point by whatever number you put in
public final class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public static Point getMidpoint(Point one, Point two) {
int x = (one.x + two.x) / 2;
int y = (one.y + two.y) / 2;
return new Point(x, y);
}
public static Point getEndpoint(Point one, Point mid) {
int x = (2 * mid.x) - one.x;
int y = (2 * mid.y) - one.y;
return new Point(x, y);
}
public static Vector2 fromEndAndMidpoint(double xa, double ya, double xmid, double ymid) {
Vector2 v = new Vector2();
v.x = 2 * xmid - xa;
v.y = 2 * ymid - ya;
return v;
}
Here is the method that shows how to compute it.
Note that this works because you know that one point is in the middle. So the desired endpoint is just one step away from the middle or two steps away from the other end.
public static Vector2 midpoint(double xe, double ye, double xm,
double ym) {
// calculate the step to go from the end point to the middle
// for both x and y.
double xstep = xm - xe;
double ystep = ym - ye;
// then just add the step to the middle to get the end point.
return new Vector2(xm + xstep, ym + ystep);
}
Note that the above will not work to find any point. For that you must simply find the equation of the line y = mx + b by computing the slope, m, and y-intercept, b.
I created a simple Application which is meant to display, in 2D, dots which are hypothetically on a 3D plane (these points are of type Vector, written below). The class uses the XYZ Coordinates of the Camera and the XYZ Coordinates of the Vector, and uses that information to quickly translate the Vector XYZ Coordinates into XY Coordinates.
The only class needed in this question is the Vector class given below. All other classes used are omitted because they essentially fire mouse movements and redraw the frame.
--My concern is that, as the camera moves, the Vector points jump around as if the formula that I'm using is totally unreliable. Are these formulas (found under Perspective Projection) completely incorrect? The use of those formulas can be found within my set2D method. Am I doing something completely wrong, skipping steps, or perhaps have I translated the formula into code incorrectly?
Thanks!
import java.awt.Color;
import java.awt.Graphics;
public class Vector
{
private int cX, cY, cZ; //Camera Coordinates
private int aX, aY, aZ; //Object Coordinates
private double bX, bY; //3D to 2D Plane Coordinates
public Vector(int aX, int aY, int aZ, int cX, int cY, int cZ)
{
this.aX = aX;
this.aY = aY;
this.aZ = aZ;
this.cX = cX;
this.cY = cY;
this.cY = cZ;
set2D();
}
//SETS
public void setCameraX(int cX)
{
this.cX = cX;
set2D();
}
public void setCameraY(int cY)
{
this.cY = cY;
set2D();
}
public void setCameraZ(int cZ)
{
this.cZ = cZ;
set2D();
}
public void setCameraXYZ(int cX, int cY, int cZ)
{
setCameraX(cX);
setCameraY(cY);
setCameraZ(cZ);
}
public void setObjX(int x)
{
this.aX = x;
}
public void setObjY(int y)
{
this.aY = y;
}
public void setObjZ(int z)
{
this.aZ = z;
}
public void setObjXYZ(int x, int y, int z)
{
this.aX = x;
this.aY = y;
this.aZ = z;
}
public void set2D()
{
//---
//the viewer's position relative to the display surface which goes through point C representing the camera.
double eX = aX - cX;
double eY = aY - cY;
double eZ = aZ - cZ;
//----
double cosX = Math.cos(eX);
double cosY = Math.cos(eY);
double cosZ = Math.cos(eZ);
double sinX = Math.sin(eX);
double sinY = Math.sin(eY);
double sinZ = Math.sin(eZ);
//---
//The position of point A with respect to a coordinate system defined by the camera, with origin in C and rotated by Theta with respect to the initial coordinate system.
double dX = ((cosY*sinZ*eY) + (cosY*cosZ*eX)) - (sinY * eZ);
double dY = ((sinX*cosY*eZ) + (sinX*sinY*sinZ*eY) + (sinX*sinY*cosZ*eX)) + ((cosX*cosZ*eY) - (cosX*sinZ*eX));
double dZ = ((cosX*cosY*eZ) + (cosX*sinY*sinZ*eY) + (cosX*sinY*cosZ*eX)) - ((-sinX*cosZ*eY) - (-sinX*sinZ*eX));
//---
//---
//The 2D projection coordinates of the 3D object
bX = (int)(((eZ / dZ) * dX) - eX);
bY = (int)(((eZ / dZ) * dY) - eY);
//---
System.out.println(bX + " " + bY);
}
//GETS
public int getCameraX()
{
return cX;
}
public int getCameraY()
{
return cY;
}
public int getCameraZ()
{
return cZ;
}
public int getObjX()
{
return aX;
}
public int getObjY()
{
return aY;
}
public int getObjZ()
{
return aY;
}
public int get2DX()
{
return (int)bX;
}
public int get2DY()
{
return (int)bY;
}
//DRAW
public void draw(Graphics g)
{
g.setColor(Color.red);
g.fillOval((int)bX, (int)bY, 3, 3);
}
//TO STRING
public String toString()
{
return (aX + " " + aY + " " + aZ);
}
}
The following line of your code does not match with the formula you are using:
double dZ = ((cosX*cosY*eZ) + (cosX*sinY*sinZ*eY) + (cosX*sinY*cosZ*eX)) - ((-sinX*cosZ*eY) - (-sinX*sinZ*eX));
Notice the part - ((-sinX*cosZ*eY) - (-sinX*sinZ*eX))
This should be - ((sinX*cosZ*eY) - (sinX*sinZ*eX))
Since if you take sinX and multiply it, the -ve sign stays outside. If however you multiple -sinX then the sign outside the brackets should become +ve.
I have a method that gets the area of a triangle, however it is returning 0.0.
public double getArea() {
//Find the length of sides
double side1 = p1.findLength(p2);
double side2 = p2.findLength(p3);
double side3 = p3.findLength(p1);
//Get area
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
My Point class with findLength(). This function works as intended in my testing:
public double findLength(Point another) {
return Math.sqrt(((another.getX() - this.getX()) * (another.getX() - this.getX()) )+
((another.getY() - this.getY()) * (another.getY() - this.getY())));
}
I can't seem to figure out why it isn't working.
Entire Point class:
/**
* Created by wilson on 9/8/2014.
*/
import java.math.*;
public class Point {
private double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double findLength(Point another) {
return Math.sqrt(((another.getX() - this.getX()) * (another.getX() - this.getX()) )+
((another.getY() - this.getY()) * (another.getY() - this.getY())));
}
}
My Test Program:
Point p1 = new Point(0,0);
Point p2 = new Point(3,3);
Point p3 = new Point(-3,-3);
Triangle2D t1 = new Triangle2D(p1, p2, p3);
System.out.println("Area of triangle: " + t1.getArea());
The "triangle" you tested:
Point p1 = new Point(0,0);
Point p2 = new Point(3,3);
Point p3 = new Point(-3,-3);
actually consists of three points all on the same line (the line x = y). Therefore, 0 is the correct answer. As far as I can tell, the code is correct.
Side note: I recommend using Math.hypot, which computes sqrt(x2 + y2) in the findLength function. Besides making findLength easier to read, it has the advantage that it won't overflow if x or y is so large that x2 or y2 won't fit in a floating-point number. (Not that it's likely you'll run into that problem, but why not do things right?)
I am in pursuit of a lightning fast java method to check if a point is inside a triangle.
I found the following c++ code in a paper from Kasper Fauerby:
typedef unsigned int uint32;
#define in(a) ((uint32&) a)
bool checkPointInTriangle(const VECTOR& point, const VECTOR& pa,const VECTOR& pb, const VECTOR& pc) {
VECTOR e10=pb-pa;
VECTOR e20=pc-pa;
float a = e10.dot(e10);
float b = e10.dot(e20);
float c = e20.dot(e20);
float ac_bb=(a*c)-(b*b);
VECTOR vp(point.x-pa.x, point.y-pa.y, point.z-pa.z);
float d = vp.dot(e10);
float e = vp.dot(e20);
float x = (d*c)-(e*b);
float y = (e*a)-(d*b);
float z = x+y-ac_bb;
return (( in(z)& ~(in(x)|in(y)) ) & 0x80000000);
}
I was wondering if this code snippet could be converted to java, and if so, if it would outperform my Java code:
public class Util {
public static boolean checkPointInTriangle(Vector p1, Vector p2, Vector p3, Vector point) {
float angles = 0;
Vector v1 = Vector.min(point, p1); v1.normalize();
Vector v2 = Vector.min(point, p2); v2.normalize();
Vector v3 = Vector.min(point, p3); v3.normalize();
angles += Math.acos(Vector.dot(v1, v2));
angles += Math.acos(Vector.dot(v2, v3));
angles += Math.acos(Vector.dot(v3, v1));
return (Math.abs(angles - 2*Math.PI) <= 0.005);
}
public static void main(String [] args) {
Vector p1 = new Vector(4.5f, 0, 0);
Vector p2 = new Vector(0, -9f, 0);
Vector p3 = new Vector(0, 0, 4.5f);
Vector point = new Vector(2, -4, 0.5f);
System.out.println(checkPointInTriangle(p1, p2, p3, point));
}
}
and the Vector class:
public class Vector {
public float x, y, z;
public Vector(float x, float y, float z) {
this.x = x; this.y = y; this.z = z;
}
public float length() {
return (float) Math.sqrt(x*x + y*y + z*z);
}
public void normalize() {
float l = length(); x /= l; y /= l; z /= l;
}
public static float dot(Vector one, Vector two) {
return one.x*two.x + one.y*two.y + one.z*two.z;
}
public static Vector min(Vector one, Vector two) {
return new Vector(one.x-two.x, one.y-two.y, one.z-two.z);
}
}
or is there an even faster method for Java?
Thanks in advance!
The code you've found, if correct, should be quite a bit faster than what you've got. The return statement
return (( in(z)& ~(in(x)|in(y)) ) & 0x80000000);
is just a tricky way of checking the sign bit of the floating point numbers; if I'm not completely wrong it's equivalent to:
return z < 0 && x >= 0 && y >= 0;
The text of the paper should confirm this. The rest I would guess you can convert yourself.