Calculate Euclidean Distance Between Two Points Using Constructor - java

I'm working on an assignment to write a java program which implements a Point data type with the following constructor:
Point(double x, double y, double z)
and, the following API:
double distanceto(Point q)
it returns the Euclidean distance between this and q.
The Euclidean distance between (x1, y1, z1) and (x2, y2, z2) is defined as sqrt( (x1-x2)^2 + (y1-y2)^2) + (z1-z2)^2).
String toString() – it returns the string representation of the point. An example would be (2.3,4.5,3.0).
Write a main method in the class that is used to test it.
It should create two Point objects using input provided by the user on the command-line.
Then it should print out the two points followed by their Euclidean distance
A sample run would be as follows.
java Point 2.1 3.0 3.5 4 5.2 3.5
The first point is (2.1,3.0,3.5)
The second point is (4.0,5.2,3.5)
Their Euclidean distance is 2.90
The program won't compile, but I'm not sure why. I'm new to programming, so I followed some steps from online and Codecademy to try and access objects in the constructor, but I think I'm doing it wrong. Any suggestions would be greatly appreciated.
public class Point {
double x1;
double y1;
double z1;
double x2;
double y2;
double z2;
public Point(double x, double y, double z){
x1 = x;
y1 = y;
z1 = z;
x2 = x;
y2 = y;
z2 = z;
}
public double distanceTo(Point q){
return Math.sqrt(Math.pow((x1-x2), 2.0) + Math.pow((y1-y2), 2.0) + Math.pow((z1-z2), 2.0));
}
double x3 = x1-x2;
double y3 = y1-y2;
double z3 = z1-z2;
public String toString() {
return "(" + x3 + ", " + y3 + ", " + z3 + ")";
}
public static void main (String[]args){
Point pointOne = new Point(args[0]);
Point pointTwo = new Point(args[1]);
Point distance = new distanceTo();
System.out.println("The first point is " + "(" + pointOne + ")");
System.out.println("The second point is " + "(" + pointTwo + ")");
System.out.println("Their Euclidean distance is " + distance);
}
}

A couple of things:
First, you should only need one set of variables for your Point class. Just make one set and construct two point objects.
In the distanceTo() method, access the other point's coordinates by doing q.x1, q.y1, and so on.
In the main method, distance should be double that's pointOne's distance to pointTwo.
Here's the code that worked for me
class Point {
double x1;
double y1;
double z1;
public Point(double x, double y, double z){
//each point object only needs one x, y and z
x1 = x;
y1 = y;
z1 = z;
}
public double distanceTo(Point pointTwo){
return Math.sqrt(Math.pow(pointTwo.x1-x1, 2.0) + Math.pow(pointTwo.y1-y1, 2.0) + Math.pow(pointTwo.z1-z1, 2.0));
//formula is sqrt((p2.x-p1)^2 + (p2.y-p1.y)^2 + (p2.z - p1.z)^2
}
public String toString() {
return "(" + x1 + ", " + y1 + ", " + z1 + ")";
//we don't need a diff set of variables here
}
public static void main (String[]args){
//make two diff points with coords
Point pointOne = new Point(2.1, 3.0, 3.5);
Point pointTwo = new Point(4.0,5.2, 3.5);
double distance = pointOne.distanceTo(pointTwo);
//uses point two as the parameter (x2, y2, z2 in formula)
System.out.println("The first point is " + "(" + pointOne + ")");
System.out.println("The second point is " + "(" + pointTwo + ")");
System.out.println("Their Euclidean distance is " + distance);
}
}

Related

Is it possible to display double with larger precision than a float via String Formatter?

How can I keep precision when working with doubles in Java? I have the following test case:
double x = -1.0 / 3.0;
double y = 1.0000000001;
Point2 p = new Point2(x, y);
String expected = "(" + x + ", " + y + ")";
assertEquals(expected, p.toString());
Point2 constructor:
public Point2(double x, double y) {
this.x = x;
this.y = y;
}
toString Override:
#Override
public String toString() {
return String.format("(%f, %f)", this.x, this.y);
}
Which is failing: org.junit.ComparisonFailure: expected:<(-0.333333[3333333333, 1.0000000001])> but was:<(-0.333333[, 1.000000])>
Problem was in my toString() method. Makes sense that they were being formatted as floats now. Ended up updated it to :
#Override
public String toString() {
return "(" + this.x + ", " + this.y + ")";
}

How do I use a JOptionPane input from a test class in another class?

I currently have a Triangle class with all of my calculations.
public class Triangle
{
private double x1;
private double y1;
private double x2;
private double y2;
private double x3;
private double y3;
private double lengthA;
private double lengthB;
private double lengthC;
private double angleA;
private double angleB;
private double angleC;
private double perimeter;
private double height;
private double area;
public double calcArea()
{
area = .5 * lengthC * height;
return area;
}
public double calcPerimeter()
{
perimeter = lengthA + lengthB + lengthC;
return perimeter;
}
public double lengthA()
{
lengthA = Math.sqrt(Math.pow((x2 - x3),2) + Math.pow(height,2));
return lengthA;
}
public double lengthB()
{
lengthB = Math.sqrt(Math.pow((x3 - x1),2) + Math.pow(height,2));
return lengthB;
}
public double lengthC()
{
lengthC = x2 - x1;
return lengthC;
}
public double getHeight()
{
height = y3 - y1;
return height;
}
public double angleA()
{
angleA = Math.abs(Math.toDegrees(Math.asin(height / lengthB)));
return angleA;
}
public double angleB()
{
angleB = Math.abs(Math.toDegrees(Math.asin(height / lengthA)));
return angleB;
}
public double angleC()
{
angleC = 180 - angleA - angleB;
return angleC;
}
}
I also have a TriangleTester class that uses JOptionPane to get coordinates for the triangle.
import javax.swing.*;
public class TriangleTester
{
public static void main (String [] args)
{
double x1;
double y1;
double x2;
double y2;
double x3;
double y3;
String v1;
String v2;
String v3;
String v4;
String v5;
String v6;
v1 = JOptionPane.showInputDialog("Enter x1 for point A");
v2 = JOptionPane.showInputDialog("Enter y1 for point A");
v3 = JOptionPane.showInputDialog("Enter x2 for point B");
v4 = JOptionPane.showInputDialog("Enter y2 for point B");
v5 = JOptionPane.showInputDialog("Enter x3 for point C");
v6 = JOptionPane.showInputDialog("Enter y3 for point C");
x1 = Integer.parseInt(v1);
y1 = Integer.parseInt(v2);
x2 = Integer.parseInt(v3);
y2 = Integer.parseInt(v4);
x3 = Integer.parseInt(v5);
y3 = Integer.parseInt(v6);
Triangle tri = new Triangle();
double lengthA = tri.lengthA();
double lengthB = tri.lengthB();
double lengthC = tri.lengthC();
double angleA = tri.angleA();
double angleB = tri.angleB();
double angleC = tri.angleC();
double perimeter = tri.calcPerimeter();
double height = tri.getHeight();
double area = tri.calcArea();
System.out.printf("Set up triangle with coordinates (" + x1 + "," + y1 + "), (" + x2 + "," + y2 + "), (" + x3 + "," + y3 + ")");
System.out.printf("\nArea:\t\t\t\t" + area);
System.out.printf("\nPerimeter:\t\t" + perimeter);
System.out.printf("\nLength side a:\t" + lengthA);
System.out.printf("\nLength side b:\t" + lengthB);
System.out.printf("\nLength side c:\t" + lengthC);
System.out.printf("\nHeight h:\t\t" + height);
System.out.printf("\nAngle A:\t\t\t" + angleA);
System.out.printf("\nAngle B:\t\t\t" + angleB);
System.out.printf("\nAngle C:\t\t\t" + angleC);
}
}
When I run the tester code, it prints the coordinates with the correct x and y values, but everything else ends up printing as 0. I believe this is because I have not made any connection between the x and y values of the Triangle class and the TriangleTester class. How can I get the Triangle class to use the inputted x and y values from the TriangleTester class to calculate the answers? Thanks.
The main idea here was you were using a getter as a setter.
You were not passing the data for the setter methods between classes
Main program
package triangles;
import javax.swing.*;
public class Triangles {
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
double x1;
double y1;
double x2;
double y2;
double x3;
double y3;
String v1;
String v2;
String v3;
String v4;
String v5;
String v6;
v1 = JOptionPane.showInputDialog("Enter x1 for point A");
v2 = JOptionPane.showInputDialog("Enter y1 for point A");
v3 = JOptionPane.showInputDialog("Enter x2 for point B");
v4 = JOptionPane.showInputDialog("Enter y2 for point B");
v5 = JOptionPane.showInputDialog("Enter x3 for point C");
v6 = JOptionPane.showInputDialog("Enter y3 for point C");
x1 = Integer.parseInt(v1);
y1 = Integer.parseInt(v2);
x2 = Integer.parseInt(v3);
y2 = Integer.parseInt(v4);
x3 = Integer.parseInt(v5);
y3 = Integer.parseInt(v6);
Triangle tri = new Triangle();
//set all needed data
tri.setLengthA(x2,x3);
tri.setLengthB(x3,x1);
tri.setLengthC(x2,x1);
tri.setHeight(y3,y1);
// set calculations off the data
tri.setAngleA();
tri.setAngleB();
tri.setAngleC();
double perimeter = tri.calcPerimeter();
double area = tri.calcArea();
System.out.printf("Set up triangle with coordinates (" + x1 + "," + y1 + "), (" + x2 + "," + y2 + "), (" + x3 + "," + y3 + ")");
System.out.printf("\nArea:\t\t\t\t" + area);
System.out.printf("\nPerimeter:\t\t" + perimeter);
System.out.printf("\nLength side a:\t" + tri.lengthA);
System.out.printf("\nLength side b:\t" + tri.lengthB);
System.out.printf("\nLength side c:\t" + tri.lengthC);
System.out.printf("\nHeight h:\t\t" + tri.height);
System.out.printf("\nAngle A:\t\t\t" + tri.angleA);
System.out.printf("\nAngle B:\t\t\t" + tri.angleB);
System.out.printf("\nAngle C:\t\t\t" + tri.angleC);
}
}
and your class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package triangles;
/**
*
* #author jstil
*/
public class Triangle {
private double x1;
private double y1;
private double x2;
private double y2;
private double x3;
private double y3;
public double lengthA;
public double lengthB;
public double lengthC;
public double angleA;
public double angleB;
public double angleC;
private double perimeter;
public double height;
private double area;
public double calcArea()
{
area = .5 * lengthC * height;
return area;
}
public double calcPerimeter()
{
perimeter = lengthA + lengthB + lengthC;
return perimeter;
}
public void setLengthA( double x2 , double x3)
{
lengthA = Math.sqrt(Math.pow((x2 - x3),2) + Math.pow(height,2));
}
public void setLengthB(double x3, double x1)
{
lengthB = Math.sqrt(Math.pow((x3 - x1),2) + Math.pow(height,2));
}
public void setLengthC(double x2, double x1)
{
lengthC = x2 - x1;
}
public void setHeight(double y3, double y1)
{
height = y3 - y1;
}
public void setAngleA()
{
angleA = Math.abs(Math.toDegrees(Math.asin(height/ lengthB)));
}
public void setAngleB()
{
angleB = Math.abs(Math.toDegrees(Math.asin(height / lengthA)));
}
public void setAngleC()
{
angleC = 180 - angleA - angleB;
}
}

Practice program that teaches me how to overload methods in Java keeps outputting the wrong answer

I understand the concept of overloading and I'd like to think I have done it succesfully in this program; it runs fine but the output is not correct.
The program is supposed to calculate the area of a circle from two points, one being the radius and the other being a random point on the outside of the circle. The two points are given by the user, and each point consists of two numbers. So point one is x1, x2, while point two is y1, y2.
I did a test run by inputting the numbers 1, 2, 3, and 4, which should give me an answer of 3.1458....(pi). However, it gives me 25.132741228718352.
Any help on figuring out what is giving me this weird output would be much appreciated.
Here is the code
import java.util.Scanner;
public class AreaCircle {
static Scanner input = new Scanner(System.in);
public static double getDistance(double x1, double y1,
double x2, double y2) {
double dx = x2 - x1;
double dy = y2 - y1;
double distanceSquared = dx * dx + dy * dy;
double radius = Math.sqrt(distanceSquared);
return radius;
}
public static double areaCircle (double radius){
double area = (double)Math.PI * (radius * radius);
return area;
}
public static double areaCircle (double x1, double x2,
double y1, double y2) {
double radius = getDistance(x1, x2, y1, y2);
double area = areaCircle (radius);
return area;
}
public static void main(String[] args) {
System.out.print("Please input two points, with the first being \n"
+ "the middle of the circle and the other being \n"
+ "a point on the outside of the circle. These two points will \n"
+ "be used to find the area of your circle. \n\n"
+ "Input the first point here: ");
double x1 = input.nextDouble();
System.out.print("Input the second point here: ");
double x2 = input.nextDouble();
System.out.print("Input the third point here: ");
double y1 = input.nextDouble();
System.out.print("Input the fourth point here: ");
double y2 = input.nextDouble();
double result = areaCircle(x1, x2, y1, y2);
System.out.println("Your result is: " + result);
}
}
You're actually calculating the distance between (1, 2) and (3,4), since you've switched x2 with y1 in distance (compare it with the area function -- you'll see what I mean).
The distance between (1,2) and (3,4) is sqrt 8, when you substitute that into the formula, it gives an area of 8 * pi ~= 25.

Java Code Visual Representation of Slope in 3D Space

My last question involved getting the syntax right, this question is:
How should I best go about showing a 3D representation of a line between 2 points? I've calculated the slope, now I simply want to make a window appear and display a cube like this: http://www.wolframalpha.com/input/?i=slope+between+%282%2C6%2C1%29+and+%283%2C5%2C0%29
public static double calcSlope(Point p1, Point p2){
//math to calculate the slope
double slope1 = (p2.getY()- p1.getY())/(p2.getX()-p1.getX());
double angle1 = Math.atan(slope1);
double distance1 = (p2.getY()-p1.getY())/Math.sin(angle1);
System.out.println("distance: " + distance1);
double slopeFinal = 1/distance1;
return slopeFinal;
}
public static void main(String[]args){
double x1, x2, y1, y2, z1, z2;
x1 = 5;
x2 = 7.5;
y1 = 3.25;
y2 = 4;
z1 = 0;
z2 = 1;
Point point1 = new Point(x1, y1, z1);
//declaring new variables of class Point
Point point2 = new Point(x2, y2, z2);
//in Point y2 is = y, but y1 is also = y Point (x,y,z) serves as a placeholder where "x" is just
//a placeholder
double slope = calcSlope(point1, point2);
double angle = Math.atan(slope);
System.out.println("Your Slope is " + slope);
System.out.println("Angle of entry is " + angle);
}
Thats a BIG question, you're going to have to use some 3rd party library for 3D graphics as java doesn't natively support it*. I would suggest you look at jmonkey engine but thats just my personal preference: http://jmonkeyengine.com/.
*technically java3D does exist but it still doesn't come with java as standard and it is a largely abandoned project

Point on Line by distance to first point

I want to calculate a point on a line by the distance to the first point.
Because I dont have any coordinates of the new point, i can't use the linear interpolation...
I thought like this:
Example Drawing
(Sorry, I'm a new user and I am not allowed to post images)
But actually it doesnt work, so I ask you for help.
Here is the actual code in java:
public static PointDouble interpolationByDistance(Line l, double d) {
double x1 = l.p1.x, x2 = l.p2.x;
double y1 = l.p1.y, y2 = l.p2.y;
double ratioP = ratioLine_x_To_y(l);
double disP = l.p1.distance(l.p2);
double ratioDis = d / disP;
PointDouble pn = l.p2.getLocation();
pn.multi(ratioDis);
System.out.println("dis: " + d);
System.out.println("new point dis: " + l.p1.distance(pn));
return pn;
}
Thank you.
As a programmer you should love changing a problem to a one you have already solved. Find the ratio and then use the linear interpolation:
public static PointDouble interpolationByDistance(Line l, double d) {
double len = l.p1.distance(l.p2);
double ratio = d/len;
double x = ratio*l.p2.x + (1.0 - ratio)*l.p1.x;
double y = ratio*l.p2.y + (1.0 - ratio)*l.p1.y;
System.out.println(x + ", " + y);
...
}
The basics of this are quite simple:
f = 0.3;
xp = f * x1 + (1-f) * x2;
yp = f * y1 + (1-f) * y2;
To understand this, consider:
if f==0, then xp = x2, yp=y2
if f==1, then xp = x1, yp=y1
for any value of f between 0..1, you get a point between (x1,y1)..(x2,y2)
I'm not sure what you exactly intend to calculate. This takes a value f in the range 0..1. If you have d as an absolute length, do f=d/disP

Categories

Resources