Hi there, I am having some syntax and logical implementation problems with a really very simple requirment and I am hopeing someone can help point me in the right direction.
The problem I am having concrens trignometric functions in 3d space and what I am trying to do is find the angle of tragectory between 2 points in 3d space. I have solved the problem on paper and con not seem to understand the syntax to implement in java. I hope the following example explains the intent a little better.

point1 = x1, y1, z1 (starting point)
point2 = x2, y2, z2 (end point)

the first step I used was to calculate the distance between each point element

xd = x2 - x1
yd = y2 - y1
zd = z2 - z1

the second step was to calculate the magnitude of the vector x1,z1 to x2,z2

Vxz = sqrroot(xd^2 + zd^2)

Vxz is then to be used to calculate the angle (a) based on trig functions

tan a = yd / Vxz

once I have this i can then use that angle to move the point2 to a given distance from point1, working in reverse by first calculating new values for yd and Vxz via

yd = distance * Cos a
Vxz = distance * Sin a

etc

I am aware that I will also need to calculate the angle of tragectory from point x1,z1 to x2,z2 in order for the new cordinates to give the required location.

This seems a long way around the problem, so if anyone has any ideas on how to speed up the process please let me know, if not any help with the syntax required for the trig functions would be greatly apreciated.

Thanks and sorry for the bad spelling.

Recommended Answers

All 12 Replies

Look up the documentation for the Math class, which has the trig functions you are looking for, in addition to square root and exponent functions. If this is not enough help, ask a more specific question.

The problem is implementing the syntax demonstrated in the help file, as I can not visualise how i would implement it. I have already implemented the square root operation.

My main concern is that java works in radians and not degrees and from some of what I have seen this impacts on implementation.

I do not how more specific you want the question to be.

Many of the Math operations are in radians, but the Math class also has methods to convert both to radians and to degrees.

When I say more specific, I mean what syntax is it that you are having trouble with? You say you already implemented the square root operation. So you are fine calculating this line?

Vxz = sqrroot(xd^2 + zd^2)

Then is it this line you need help with?

tan a = yd / Vxz

If so, you can compute the tangent of an angle using Math.tan(/* put the angle here */);

ok maybe i did not make it clear enough that the example was the paper solution. and it is the tan / cos and sin statements I am having a problem with.

As such, knowing the adjacent and the oposit values how do I convert that to to radions to then convert to tangent?

Say you have an angle in degrees.

double angle ...  // in degrees
double radianAngle = Math.toRadians(angle);
double radianTangent = Math.tan(radianAngle);
double radianDegrees = Math.toDegrees(radianTangent);

Is that what you're looking for?

Say you have an angle in degrees.

double angle ...  // in degrees
double radianAngle = Math.toRadians(angle);
double radianTangent = Math.tan(radianAngle);
double radianDegrees = Math.toDegrees(radianTangent);

Is that what you're looking for?

yes to some degree i am not sure if this will function

//To get value of angle
double angle = Math.atan2(xd,zd);

or do i need to use somthing like

//If given in radians
double angle = Math.atan2(xd,zd)*180/Math.PI)

or are they both completly wrong????

First question, are xd and zd in degrees or radians?

Second, I would use Math.toDegrees and Math.toRadians rather than trying to do the math (*180/Math.PI) myself.

If xd and zd are already in radians, then you can do this:

double angle = Math.toDegrees(Math.atan2(xd,zd));

xd, zd and yd are calculated distances between each point coordinate, as explained in the original post, as such they are a value, not degrees or radians.

OK, sorry. I'm a programmer, not a mathematician.

Did I answer your question about how to calculate the tangent?

Thank you for the reply but not completly. However the discussion is being very helpful. I am still uncertain and will try some experiments with it when I get the chance. I am concered about the number of computations that it will take to carry out this process for a large number of points and hope to find another method that will not be so heavy.

OK, glad the discussion was helpful. Good luck with your experiments.

Thank you to those that have aided me in this thread. For completness I have posted the final code implementation and will look to enhance it in the future

public class Vertex 
{
	private float xPos;
	private float yPos;
	private float zPos;
	
	public Vertex(float point1, float point2, float point3) {
		xPos = point1;
		yPos = point2;
		zPos = point3;
	}

	public float getXPos()
	{
		return xPos;
	}
	
	public float getYPos()
	{
		return yPos;
	}
	
	public float getZPos()
	{
		return zPos;
	}
	
	public void moveDistanceFormPoint(Vertex origin, float distance)
	{
		//Method to move the vertex a specified distance from a given point
		//calculate component distances from point
		float xDistance = xPos - origin.getXPos();
		float yDistance = yPos - origin.getYPos();
		float zDistance = zPos - origin.getZPos();
		//Calculate current distance from point
		//calculate xz edge
		float xzDistance = (float)Math.sqrt((xDistance * xDistance)+ (zDistance * zDistance));
		//Calculate angle of elevation
		double elevationAngle = Math.atan2(yDistance, xzDistance);
                //Calculate new vertex position
		//Calculate new y position
		float newYDistance = (float) Math.sin(elevationAngle) * distance;
		yPos = yPos - (yDistance - newYDistance);
		//Calculate new x z vector magnitude
		xzDistance = (float) Math.cos(elevationAngle) * distance;
		//Calculate angle of perspective (on x z plain)
		double perAngle = Math.atan2(zDistance, xDistance);
		float newXDistance = (float) Math.cos(perAngle) * xzDistance;
		float newZDistance = (float) Math.sin(perAngle) * xzDistance;
		//Calculate new x and z positions
		xPos = xPos - (xDistance - newXDistance);
		zPos = zPos - (zDistance - newZDistance);	
	}
	
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.