For this code, I am trying to determine the distance between (x1, y1) and (x2, y2). The equation for the distance is sqrt(x2 - x1)^2 + (y2 - y1)^2.
The code looks like this,
import java.util.Scanner;
public class CoordinateGeometry {
public static void main(String [] args) {
Scanner scnr = new Scanner(System.in);
double x1;
double y1;
double x2;
double y2;
double pointsDistance;
double xDist;
double yDist;
pointsDistance = 0.0;
xDist = 0.0;
yDist = 0.0;
x1 = scnr.nextDouble();
y1 = scnr.nextDouble();
x2 = scnr.nextDouble();
y2 = scnr.nextDouble();
poinsDistance = Math.sqrt(Math.pow(x2 - x1, 2) + (Math.pow(y2 - y1, 2));
System.out.println(pointsDistance);
}
}
I keep getting an error, CoordinateGeometry.java:23: error: ')' expected
poinsDistance = Math.sqrt(Math.pow(x2 - x1, 2) + (Math.pow(y2 - y1, 2));
^
1 error
What does this error mean?
Also an example would be, for points (1.0, 2.0) and (1.0, 5.0), pointsDistance is 3.0.
You are missing closing ) at the end of line
poinsDistance = Math.sqrt(Math.pow(x2 - x1, 2) + (Math.pow(y2 - y1, 2)));
Or remove the opening ( before Math.pow.
Your code should look like this:
poinsDistance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
Related
My code works. It is giving me the correct result, but I feel I did a mistake because I declare x1, y1, x2, y2 too often (globally and locally). Am I? If I, however, delete one of the declarations, it does not work anymore. Error message:
error: cannot find symbol
Maybe someone can explain to me, how I should have solved the problem without declaring x1, y1, x2, y2 that often.
public class Distanz {
public static void main(String[] args) {
double d = 0;
double x1 = 10;
double y1 = 8;
double x2 = 2;
double y2 = 12;
berechneDistanzAlsProzedur(x1, x2, y1, y2);
System.out.print("Distanz von p1 und p2 berechnet mit einer Funktion: " + berechneDistanzAlsFunktion(d));
}
public static void berechneDistanzAlsProzedur(double x1, double x2, double y1, double y2) {
x1 = 10;
y1 = 8;
x2 = 2;
y2 = 12;
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: " + Math.sqrt(sqd_d));
}
public static double berechneDistanzAlsFunktion(double d) {
double x1 = 10;
double y1 = 8;
double x2 = 2;
double y2 = 12;
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
return (Math.sqrt(sqd_d));
}
}
You can simply use the parameters x1, y1, x2, and y2 instead. Otherwise, your methods will always return the same value regardless of the arguments it was called with.
public static void berechneDistanzAlsProzedur(double x1, double x2, double y1, double y2) {
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: "+Math.sqrt(sqd_d));
}
public static double berechneDistanzAlsFunktion(double x1, double x2, double y1, double y2) {
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
return (Math.sqrt(sqd_d));
}
You could go simple and declare global variables like this:
public class Distanz {
public static void main(String[] args) {
double d = 0;
berechneDistanzAlsProzedur();
System.out.print("Distanz von p1 und p2 berechnet mit einer Funktion: "+berechneDistanzAlsFunktion(d));
}
static double x1 = 10;
static double y1 = 8;
static double x2 = 2;
static double y2 = 12;
public static void berechneDistanzAlsProzedur() {
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: "+Math.sqrt(sqd_d));
}
public static double berechneDistanzAlsFunktion(double d) {
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
return (Math.sqrt(sqd_d));
}
}
Of course, I might not know what you want at all and my answer could be completely useless but I think I answered it pretty well. :D
Below solution can eliminate static declaration and code will look very neat.
public class Distanz {
public static void main(String[] args) {
Point point = new Point(10, 8, 2, 12);
berechneDistanzAlsProzedur(point);
System.out.print("Distanz von p1 und p2 berechnet mit einer Funktion: " + berechneDistanzAlsFunktion(point));
}
public static void berechneDistanzAlsProzedur(Point point) {
double sqd_d = (point.getX1() - point.getX2()) * (point.getX1() - point.getX2()) + (point.getY1() - point.getY2()) * (point.getY1() - point.getY2());
//This formula is same which you have used in berechneDistanzAlsFunktion method, you can eliminate berechneDistanzAlsProzedur function and directly call berechneDistanzAlsFunktion in main function
//You can pass different points to validate different result
System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: " + Math.sqrt(sqd_d));
}
public static double berechneDistanzAlsFunktion(Point point) {
return (Math.sqrt((point.getX1() - point.getX2()) * (point.getX1() - point.getX2()) + (point.getY1() - point.getY2()) * (point.getY1() - point.getY2())));
}
}
Your Point class should be look like this:
public class Point {
private final double x1;
private final double y1;
private final double x2;
private final double y2;
public Point(double x1, double y1, double x2, double y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public double getX1() {
return x1;
}
public double getY1() {
return y1;
}
public double getX2() {
return x2;
}
public double getY2() {
return y2;
}
}
Here Point.java called as Wrapper class, which is holding Points information in this code.
Happy Coding.
On the line where I call getDistance(points[i], points[j]) I am getting error asking my to change my getDistance method parameters to doubles instead of arrays, though I thought I was passing an array to the method due to how multidimensional arrays work.
public static void main(String[] args) {
double[][] points = {
{1.0, 2.0, 3.0},
{0.0, 0.0, 2.0},
{1.0, 1.5, 4.0},
{3.0, 2.0, 1.0}
};
for(int i=0; i<points.length; i++){
for(int j=1; j<points[0].length; j++){
getDistance(points[i], points[j]);
}
}
}
public double getDistance(Array points1[], Array points2[]){
double x1 = Array.getDouble(points1, 0);
double x2 = Array.getDouble(points2, 0);
double y1 = Array.getDouble(points1, 1);
double y2 = Array.getDouble(points2, 1);
double z1 = Array.getDouble(points1, 2);
double z2 = Array.getDouble(points2, 2);
double distance = Math.sqrt(Math.pow(x1 - x2, 2) +(Math.pow(y1 - y2, 2) +
(Math.pow(z1 - z2, 2))));
return distance;
}
Your method getDistance defines the two parameters as type Array where as where you call it the type is double[] which is not the same.
Rewrite your getDistance as follows;
public double getDistance(double[] points1, double[] points2){
double x1 = points1[0];
double x2 = points2[0];
double y1 = points1[1];
double y2 = points2[1];
double z1 = points1[2];
double z2 = points2[2];
double distance = Math.sqrt(Math.pow(x1 - x2, 2) +(Math.pow(y1 - y2, 2) +
(Math.pow(z1 - z2, 2))));
return distance;
}
Your method should declare an array the same as you did in your main.
Now:
Instead of public double getDistance(Array points1[], Array points2[])
Should be:
public double getDistance(double points1[], double points2[])
So I've been trying to implement Perlin noise recently, and have run into some unusual problems. Whenever the edges of the grid in which the random vectors are stored are crossed, the derivative appears to be discontinuous.
Here's a link to a picture of the output (on the right), along with a 1 dimensional slice (on the left).
The Output
class perlin{
private double[][][] grid;
public perlin(int x,int y, int seed){
Random r = new Random(seed);
grid = new double[x+1][y+1][2];
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[0].length;j++){
grid[i][j][0] = 2*r.nextDouble()-1;
grid[i][j][1] = 2*r.nextDouble()-1;
}
}
}
public static double lerp(double a, double b, double t){
double c = t * t * t * (t * (t * 6 - 15) + 10);
return (b * c) + (a * (1 - c));
}
public double get(double x, double y){
double x2;
double y2;
double x3;
double y3;
x2 = x * (grid.length-1);
y2 = y * (grid[0].length-1);
x3 = down(x2);
y3 = down(y2);
x2 = x2 - x3;
y2 = y2 - y3;
int i = (int) (x3);
int j = (int) (y3);
return lerp(lerp(dot(x2, y2, grid[i][j][0], grid[i][j][1] ), dot(1 - x2, y2, grid[i + 1][j][0], grid[i + 1][j][1]),x2), lerp(dot(x2, 1 - y2, grid[i][j + 1][0], grid[i][j +1][1] ), dot(1 - x2,1 - y2, grid[i + 1][j + 1][0], grid[i + 1][j + 1][1] ), x2),y2 );
// return 0;
}
public static double dot(double x1, double y1, double x2, double y2){
return x1 * x2 + y1 * y2;
}
private static double down(double a){
if (a == 0){
return 0;
}
if(a == Math.floor(a)){
return a - 1;
}else{
return Math.floor(a);
}
}
}
From what I understand about the math behind this, the derivative of the noise should be continuous at all points, but that does not appear to be the case.
I have an assignment for school where I have to create a program that will calculate the area of a circle given four points, but when I invoke the method areaCircle in main, nothing happens. It doesn't calculate the area.
public static void main(String[] args) {
Scanner reader;
reader = new Scanner(System.in);
System.out.println("Enter the coordinates of a point on the outside of a circle.");
System.out.println("x-coordinate: ");
int x1 = reader.nextInt();
System.out.println("y-coordinate: ");
int y1 = reader.nextInt();
System.out.println("Enter the center point of the circle.");
System.out.println("x-coordinate: ");
int x2 = reader.nextInt();
System.out.println("y-coordinate:");
int y2 = reader.nextInt();
areaCircle(x1, y1, x2, y2);
}
public static double distance(int x1, int y1, int x2, int y2) {
double dx = x2 - x1;
double dy = y2 - y1;
double dsquared = dx * dx + dy * dy;
double result = Math.sqrt(dsquared);
return result;
}
public static double areaCircle(int radius, double area) {
area = Math.PI * (radius * radius);
return area;
}
public static double areaCircle(int x1, int x2, int y1, int y2) {
double radius = distance(x1, y1, x2, y2);
double area = Math.PI * (radius * radius);
return area;
}
You need only to print the result, at the moment you are only calculating it.
Instead of
areaCircle (x1, y1, x2, y2);
write
System.out.println("The area is: " + areaCircle (x1, y1, x2, y2));
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.