I have forgot my old math from school.
I have made a java class that should calculate the distance between x,y,z and origo.
But have made a wrong turn somewhere and cant get out…
The calculation doesnt give the right value.
Can you help see what i have made wrong?
Its the distance calculation thats is wrong.
import java.io.PrintWriter;
import java.util.Scanner;
public class U3point {
public static void main(String[] args) {
System.out.println("Values for x,y,z ");
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out, true);
double x;
double y;
double z;
String[] koordinater = null;
String rad = in.nextLine();
System.out.println("toString: ");
while (!rad.equals("")) {
koordinater = rad.split("\\s");
x = Double.parseDouble(koordinater[0]);
y = Double.parseDouble(koordinater[1]);
z = Double.parseDouble(koordinater[2]);
Point p = new Point(x, y, z);
out.println(p);
//call method for origo
double d = p.getDistanceToOrigo();
System.out.println("Distance to Origo: " + d);
// call method for distance
double d2 = p.getDistanceTo(x, y, z);
System.out.println("Distance: " + d2);
System.out.println("Exit program with enter or new values");
rad = in.nextLine();
}
}
}
class Point {
private double x;
private double y;
private double z;
public Point(double x1, double y1, double z1) {
x = x1;
y = y1;
z = z1;
}
public void setX(double x1) {
x = x1;
}
public void setY(double y1) {
y = y1;
}
public void setZ(double z1) {
z = z1;
}
public double[] getCoordinates() {
return new double[] { x, y, z };
}
public double getDistanceTo(double x2, double y2, double z2) {
return (Math.sqrt(((x - x2) * (x - x2) + (y - y2) * (y - y2) + (z - z2) * (z - z2))));
}
public double getDistanceToOrigo()
{
return (Math.sqrt(((x) * (x) + (y) * (y) + (z) * (z))));
}
public String toString() {
return " x: =" + x + " y:=" + y + " z:=" + z;
}
}
The problem you were calculating the distance to the same point, here are an approach for you:
import java.io.PrintWriter;
import java.util.Scanner;
public class U3point {
public static void main(String[] args) {
PrintWriter out = new PrintWriter(System.out, true);
Point p1 = generatePoint();
Point p2 = generatePoint();
out.println(p1);
out.println(p2);
// call method for origo double
double d = p1.getDistanceToOrigo();
System.out.println("Distance to Origo: " + d);
// call method for distance
double d2 = p1.getDistanceTo(p2.getX(), p2.getY(), p2.getZ());
System.out.println("Distance: " + d2);
System.out.println("The end!!");
}
public static Point generatePoint() {
System.out.println("Values for x,y,z ");
String[] koordinater = null;
Scanner in = new Scanner(System.in);
String rad = in.nextLine();
Point p = null;
koordinater = rad.split("\\s");
double x = Double.parseDouble(koordinater[0]);
double y = Double.parseDouble(koordinater[1]);
double z = Double.parseDouble(koordinater[2]);
p = new Point(x, y, z);
return p;
}
}
class Point {
private double x;
private double y;
private double z;
public Point(double x1, double y1, double z1) {
x = x1;
y = y1;
z = z1;
}
public void setX(double x1) {
x = x1;
}
public void setY(double y1) {
y = y1;
}
public void setZ(double z1) {
z = z1;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
public double[] getCoordinates() {
return new double[] { x, y, z };
}
public double getDistanceTo(double x2, double y2, double z2) {
return (Math.sqrt(((x - x2) * (x - x2) + (y - y2) * (y - y2) + (z - z2) * (z - z2))));
}
public double getDistanceToOrigo() {
return (Math.sqrt(((x) * (x) + (y) * (y) + (z) * (z))));
}
public String toString() {
return " x: =" + x + " y:=" + y + " z:=" + z;
}
}
Output:
Values for x,y,z
1 2 3
Values for x,y,z
4 5 6
x: =1.0 y:=2.0 z:=3.0
x: =4.0 y:=5.0 z:=6.0
Distance to Origo: 3.7416573867739413
Distance: 5.196152422706632
The end!!
Related
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;
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I can't seem to get my transform method right. Any suggestions on how I could make the method translate the point better? i.e. when this method is invoked, it should be able to give the new point. Also I'm having issues creating the slope method...I know slope is y2-y1/x2-x1 but how would I make that into a method. Is there a Math class I can import for the slope? Thanks much appreciated
import java.util.*;
public class Point {
private int x; // to store variables for x & y
private int y;
private double slope;
//default constructor
public Point () {
double x = 0;
double y = 0;
}
//alternate constructor
public Point (double x1, double y1) {
double x = x1;
double y = y1;
}
//set coordinates
public void setCoord (double x1, double y1){
double x = x1;
double y = y1;
}
//print method
public void print () {
System.out.print(x + "," + y);
}
//toString Method
public String toString() {
return "(" + x + "," + y + ")";
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void equals () {
if (x==y)
System.out.println(" Coordinates are the same ");
else {
System.out.println(" Coordinates are different ");
}
}
public void copy(Point temp) {
x=temp.x;
y=temp.y;
}
public Point getCopy() {
Point temp = new Point();
temp.x = x;
temp.y = y;
return temp;
}
public void distanceFromOrigin(int x1, int y1) {
double dx = x-x1;
double dy = y-y1;
}
//calculate the distance from one point to another
public void distance (double x1, double y1) {
double distance = Math.sqrt((x * x1) + (y * y1));
}
//shift the location of a point by a given amount
public void transform (double dx, double dy) {
double transform = ((x+dx) (y+dy));
}
// returns true if any given point lines up horizontally with a given point.
public boolean isHorizontal () {
return true;
}
// returns true if any given point lines up vertically with a given point.
public boolean isVertical () {
return true;
}
// returns the slope of the line
public double slope() {
return slope;
}
}
public void equals (Object o) {
if(o instanceof Point) {
Point p = (Point)o;
return p.x == x && p.y == y;
} else {
return false;
}
}
public Point getCopy() {
return new Point(x, y);
}
public double slope(Point otherPoint) {
return ((double)(otherPoint.y - y)) / ((double)(otherPoint.x / x));
}
public double distance (Point otherPoint) {
double dx = otherPoint.x - x;
double dy = otherPoint.y - y;
return Math.sqrt((dx * dx) + (dy * dy));
}
public double distanceFromOrigin() {
return distance(new Point(0, 0));
}
public boolean isHorizontal (Point otherPoint) {
return otherPoint.y == y;
}
public boolean isVertical(Point otherPoint) {
return otherPoint.x == x;
}
public void transform (double dx, double dy) {
x += dx;
y += dy;
}
I'm working on a lab for school and I have it almost completed, but there's one part that I can't get to work. The inheritance works except when I get to Cube. For some reason, it won't calculate the Area or Volume (it just comes up with 0). I'm thinking it's a problem with the way I have the inheritance from Square to Cube. Help would be awesome!
package InheritanceTest;
import javax.swing.JOptionPane;
public class InheritanceTest {
public static void main(String[] args) {
String input = "";
Point point = new Point();
input = getinput("Set variable X");
point.setx(input);
input = getinput("Set variable Y");
point.sety(input);
System.out.println("Point, x = " + point.getx() + " y = " + point.gety());
Square square = new Square();
input = getinput("Set variable Side Length");
square.setSideLength(input);
System.out.println("Square, x = " + point.getx() + " y = " + point.gety()
+ " Area = " + square.getAreaOfSquare() + " Perimeter = "
+ square.getPerimeterOfSquare());
Cube cube = new Cube();
input = getinput("Set variable depth");
cube.setDepth(input);
System.out.println("cube, x = " + point.getx() + " y = " + point.gety()
+ " Depth = " + cube.getDepth() + " Area = " + cube.getAreaOfCube()
+ " Volume = " + cube.getVolumeOfCube());
}
private static String getinput(String string) {
String x = JOptionPane.showInputDialog(string);
return x;
}
}
package InheritanceTest;
public class Cube extends Square {
private int depth;
Cube() {
super();
depth = 0;
}
Cube(int x, int y, int sideLength, int d) {
super(x, y, sideLength);
this.depth = d;
}
public int getAreaOfCube() {
return (6 * sideLength * sideLength);
}
public int getVolumeOfCube() {
return (sideLength * sideLength * sideLength);
}
public String getDepth() {
return Integer.toString(depth);
}
public void setDepth(String i) {
depth = Integer.parseInt(i);
}
}
package InheritanceTest;
public class Point {
private int x;
private int y;
Point() {
x = 0;
y = 0;
}
Point(int x, int y) {
this.x = x;
this.y = y;
}
public String getx() {
return Integer.toString(x);
}
public String gety() {
return Integer.toString(y);
}
public void setx(String input) {
x = Integer.parseInt(input);
}
public void sety(String input) {
y = Integer.parseInt(input);
}
}
package InheritanceTest;
public class Square extends Point {
protected int sideLength;
Square() {
super();
sideLength = 0;
}
Square(int x, int y, int l) {
super(x, y);
this.sideLength = l;
}
public int getAreaOfSquare() {
return sideLength * sideLength;
}
public int getPerimeterOfSquare() {
return sideLength + sideLength;
}
public String getSideLength() {
return Integer.toString(sideLength);
}
public void setSideLength(String input) {
sideLength = Integer.parseInt(input);
}
}
When you create cube (new Cube()) you aren't setting the side length (or x and y) for the Square Object it extends.
Cube(){
// This is the constructor called.
super();
depth = 0;
}
Cube(int x, int y, int sideLength, int d){
super(x, y, sideLength);
this.depth = d;
}
You probably want extract the x,y and length values into variables and use "new Cube(x, y, length, depth)"
Something like the following
String x = getinput("Set variable X");
String y = getinput("Set variable Y");
String sideLength = getinput("Set variable Side Length");
String depth getinput("Set variable depth");
Cube cube = new Cube(x, y, sideLength, depth);
Look at how you are defining getVolumeOfCube(). You are calculating volume with sideLength, but you never set sideLength to any non-zero value. Change sideLength to depth and you will get the value you are looking for.
As the title suggests, I'm working on a homework assignment where we are limited to using multi-dimensional arrays in order to create a program that finds two points nearest to each other in a three dimensional space. So far my code looks like this (hybridized from examples in my textbook and my own code):
package exercise7_7;
public class Exercise7_7 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Enter the number of points:");
int numberOfPoints = input.nextInt();
double[][] points = new double[numberOfPoints][3];
System.out.println("Enter " + numberOfPoints + " points:");
for (int i = 0; i < points.length; i++) {
points[i][0] = input.nextDouble();
points[i][1] = input.nextDouble();
points[i][2] = input.nextDouble();
}
int p1 = 0, p2 = 1, p3 = 2;
double shortestDistance = distance(points[p1][0] , points[p1][1] , points[p1][2] ,
points[p2][0] , points[p2][1] , points[p2][2]);
for (int i = 0; i < points.length; i++) {
for (int j = i + 1; j < points.length; j++) {
double distance = distance(points[i][0] , points[j][0] , points[j][1] , points[j][2] , points[i][2] , points[j][2]);
if (shortestDistance > distance) {
p1 = i;
p2 = j;
shortestDistance = distance;
}
}
}
System.out.println("The closest two points are " + "(" + points[p1][0] + "," + points[p1][1] +
and (" + points[p2][0] + "," );
}
public static double distance(
double x1, double y1, double z1, double x2, double y2, double z2) {
return Math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)) + ((z2 - z1) * (z2 - z1)));
}
}
What I mostly need help with is figuring out just how to get these points compared. I don't think the way I tackled this problem was the best way to do it.
Thanks for the help guys. I'm running on 2 hours of sleep for 2 days now so please excuse any stupid questions or sloppy code.
******
I think I've got it:
package exercise7_7;
public class Exercise7_7 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Enter the number of points:");
int numberOfPoints = input.nextInt();
double[][] points = new double[numberOfPoints][3];
System.out.println("Enter " + numberOfPoints + " points:");
for (int i = 0; i < points.length; i++) {
points[i][0] = input.nextDouble();
points[i][1] = input.nextDouble();
points[i][2] = input.nextDouble();
}
int p1 = 0, p2 = 1;
double shortestDistance = distance(points[p1][0] , points[p1][1] , points[p1][2] ,
points[p2][0] , points[p2][1] , points[p2][2]);
for (int i = 0; i < points.length; i++) {
for (int j = i + 1; j < points.length; j++) {
double distance = distance(points[i][0] , points[j][0] , points[j][1] , points[j][2] , points[i][2] , points[j][2]);
if (shortestDistance > distance) {
p1 = i;
p2 = j;
shortestDistance = distance;
}
}
}
System.out.println("The closest two points are " + "(" + points[p1][0] + "," + points[p1][1] + "," + points[p1][2] +
") and (" + points[p2][0] + "," + points[p2][1] + "," + points[p2][2] + ")");
}
public static double distance(
double x1, double y1, double z1, double x2, double y2, double z2) {
return Math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)) + ((z2 - z1) * (z2 - z1)));
}
}
Input is taken in, processed, and then outputs the two closest points. Just as a reference, when:
(-1,0,3),(-1,-1,-1),(4,1,1),(2,0.5,9),(3.5,1.5,3),(-1.5,4,2),(5.5,4,-0.5) are inputted, the outcome
seems to be (-1,0,3) and (4,1,1). Could someone confirm that for me.
If this isn't the way to follow up on my own question, I apologize. First day on these slopes and I'm still
learning the ropes.
Use a class to represent your points. This way to you have a distanceTo method that calculates and returns distance. Also you can have a toString method that prints out the point for display to the user. Taking your code rearranging yields this class:
public class ThreeDPoint {
final double x;
final double y;
final double z;
public ThreeDPoint(final double x, final double y, final double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double distanceto(final ThreeDPoint other) {
final double dx = other.x - x;
final double dy = other.y - y;
final double dz = other.z - z;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
#Override
public String toString() {
return "{X=" + x + ",Y=" + y + ",Z=" + z + "}";
}
}
Now putting that together gives this, which is much more readable. I have removed the bit where you read points and used random numbers:
public static void main(String args[]) {
final ThreeDPoint[] points = new ThreeDPoint[5];
final Random random = new Random();
for (int i = 0; i < points.length; ++i) {
points[i] = new ThreeDPoint(random.nextInt(100), random.nextInt(100), random.nextInt(100));
}
//store min
double min = Double.POSITIVE_INFINITY;
int first = -1;
int last = -1;
for (int i = 0; i < points.length; ++i) {
for (int j = i + 1; j < points.length; ++j) {
final double d = points[i].distanceto(points[j]);
if (d < min) {
min = d;
first = i;
last = j;
}
}
}
System.out.println("The minimum distance is between point " + first + " and " + last + "(" + points[first] + " and " + points[last] + "). This distance is " + min + ".");
}
private static final class ThreeDPoint {
final double x;
final double y;
final double z;
public ThreeDPoint(final double x, final double y, final double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double distanceto(final ThreeDPoint other) {
final double dx = other.x - x;
final double dy = other.y - y;
final double dz = other.z - z;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
#Override
public String toString() {
return "{X=" + x + ",Y=" + y + ",Z=" + z + "}";
}
}
My current triangle class looks like:
public class TriangleEquilateral {
private Point cornerA;
private Point cornerB;
private Point cornerC;
private double x1 = 0;
private double y1 = 0;
private double x2 = 10;
private double y2 = 0;
private double x3 = 5;
private double y3 = Math.sqrt(75);
public TriangleEquilateral(){
cornerA = new Point(x1,y1);
cornerB = new Point(x2,y2);
cornerC = new Point(x3,y3);
}
public TriangleEquilateral(double X1,double Y1,double X2,double Y2,double X3,double Y3){
x1 = X1;
y1 = Y1;
x2 = X2;
y2 = Y2;
x3 = X3;
y3 = Y3;
cornerA = new Point(X1,Y1);
cornerB = new Point(X2,Y2);
cornerC = new Point(X3,Y3);
}
public boolean isEquilateral(){
double lengthAB = Math.sqrt(Math.pow(x1-x2,2) + Math.pow(y1-y2,2));
double lengthBC = Math.sqrt(Math.pow(x2-x3,2) + Math.pow(y2-y3,2));
double lengthCA = Math.sqrt(Math.pow(x3-x1,2) + Math.pow(y3-y1,2));
boolean isEquilateral = false;
if(lengthAB == lengthBC && lengthBC == lengthCA && lengthCA == lengthAB){
isEquilateral = true;
}
System.out.println(lengthAB);
System.out.println(lengthBC);
System.out.println(lengthCA);
return isEquilateral;
}
public double sideLength(){
double sL = 0;
if(this.isEquilateral() == true){
sL = Math.sqrt(Math.pow(x1-x2,2) + Math.pow(y1-y2,2));
}
return sL;
}
How would I determine the coordinates of the midpoint of an equilateral triangle? I know that the midpoint is x = (base/2), y = heigth/2 but this only works if the base is horizontal(two corners have the same y value)
For equilateral triangle, coordinates of the triangle's center are the same as the coordinates of the center of its incircle.
Look up the formula for the incircle's center on Wikipedia:
{ (aXa+bXb+cXc)/(a+b+c), (aYa+bYb+cYc)/(a+b+c) }
Since a = b = c, it is easy to see that the coordinates of the center of an equilateral triangle are simply
{ (x0+x1+x2)/3, (y0+y1+y2)/3 }
This is more of a Math question than a java question. Anyway, to find the barycenter:
x = (x1 + x2 + x3) / 3
y = (y1 + y2 + y3) / 3