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.
Related
I have been trying to write a simple but flexible class that holds some values of generic type T.
T extends Number, which means I just want this class to deal with everything from bytes to longs.
I am not all that familiar with how to use generics, so my main question to you guys is if there's a way to shorten the following set of functions into one function in order to reduce the unnecessary code duplication. The following is the given code:
public static byte distanceSq(byte x1, byte y1, byte x2, byte y2) {
x1 -= x2;
y1 -= y2;
return (byte) (x1 * x1 + y1 * y1);
}
public static short distanceSq(short x1, short y1, short x2, short y2) {
x1 -= x2;
y1 -= y2;
return (short) (x1 * x1 + y1 * y1);
}
public static int distanceSq(int x1, int y1, int x2, int y2) {
x1 -= x2;
y1 -= y2;
return (int) (x1 * x1 + y1 * y1);
}
public static float distanceSq(float x1, float y1, float x2, float y2) {
x1 -= x2;
y1 -= y2;
return (float) (x1 * x1 + y1 * y1);
}
public static double distanceSq(double x1, double y1, double x2, double y2) {
x1 -= x2;
y1 -= y2;
return (double) (x1 * x1 + y1 * y1);
}
public static long distanceSq(long x1, long y1, long x2, long y2) {
x1 -= x2;
y1 -= y2;
return (long) (x1 * x1 + y1 * y1);
}
I have tried to write something along the lines of:
public static <U extends Number> U distanceSq(U x1, U y1, U x2, U y2) {
x1 -= x2;
y1 -= y2;
return (x1 * x1 + y1 * y1);
}
However, since the variables are objects now, the operators cannot resolve them. I tried to convert them into their appropriate wrapper using an instanceof statement, but that got me nowhere either.
You can define your method like this
public static <T extends Number> Number distanceSq(T x1,T y1,T x2,T y2){
double x = x1.doubleValue() - x2.doubleValue();
double y = y1.doubleValue() - y2.doubleValue();
return (x * x + y * y);
}
And it can be called as
Integer r1 = distanceSq(a1, b1, a2, b2).intValue();
Byte r2 = distanceSq(x1, y1, x2, y2).byteValue();
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));
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'm trying to draw Sierpinski's Triangle recursively in Java, but it doesn't work, though to me the logic seems fine. The base case is when the triangles are within 2 pixels of each other, hence the use of the Distance Formula.
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.awt.Canvas;
public class Triangle extends Canvas implements Runnable
{
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
public Triangle()
{
setBackground(Color.WHITE);
}
public void paint( Graphics window )
{
window.setColor(Color.BLUE);
window.setFont(new Font("ARIAL",Font.BOLD,24));
window.drawString("Serpinski's Gasket", 25, 50);
triangle(window, (WIDTH-10)/2, 20, WIDTH-40, HEIGHT-20, 40, HEIGHT-20, 4);
}
public void triangle(Graphics window, int x1, int y1, int x2, int y2, int x3, int y3, int r)
{
//if statement base case
//midpoint = (x1 + x2 / 2), (y1 + y2/ 2)
if(Math.sqrt((double)(Math.pow(x2-x1, 2)) + (double)(Math.pow(y2-y1, 2))) > 2)
//if(r==0)
{
window.drawLine(x1, y1, x2, y2);
window.drawLine(x2, y2, x3, y3);
window.drawLine(x3, y3, x1, y1);
}
int xa, ya, xb, yb, xc, yc; // make 3 new triangles by connecting the midpoints of
xa = (x1 + x2) / 2; //. the previous triangle
ya = (y1 + y2) / 2;
xb = (x1 + x3) / 2;
yb = (y1 + y3) / 2;
xc = (x2 + x3) / 2;
yc = (y2 + y3) / 2;
triangle(window, x1, y1, xa, ya, xb, yb, r-1); // recursively call the function using the 3 triangles
triangle(window, xa, ya, x2, y2, xc, yc, r-1);
triangle(window, xb, yb, xc, yc, x3, y3, r-1);
}
public void run()
{
try{
Thread.currentThread().sleep(3);
}
catch(Exception e)
{
}
}
}
The Runner is
import javax.swing.JFrame;
public class FractalRunner extends JFrame
{
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
public FractalRunner()
{
super("Fractal Runner");
setSize(WIDTH+40,HEIGHT+40);
getContentPane().add(new Triangle());
setVisible(true);
}
public static void main( String args[] )
{
FractalRunner run = new FractalRunner();
}
}
To me this should work but it causes a runtime/StackOverFlow error that I don't know how to correct. Any help?
You need to move the recursive calls to triangle, and the associated math, inside the conditional check on the separation. Right now, it will always call it and therefore you get the stack overflow.
Chances are your base case might not be working properly- what if the distance between two triangles is never two pixels? say we star with y1 and x1 being 0 and 200. their midpoint would be 100, then 50, 25, 12, 6, 3, 1<--- never hits the 2 pixel base case...
"StdDraw" was taken from here:
public class Sierpinski {
public static void sierpinski(int n) {
sierpinski(n, 0, 0, 1);
}
public static void sierpinski(int n, double x, double y, double size) {
if (n == 0) return;
//compute triangle points
double x0 = x;
double y0 = y;
double x1 = x0 + size;
double y1 = y0;
double x2 = x0 + size / 2;
double y2 = y0 + (Math.sqrt(3)) * size / 2;
// draw the triangle
StdDraw.line(x0, y0, x1, y1);
StdDraw.line(x0, y0, x2, y2);
StdDraw.line(x1, y1, x2, y2);
StdDraw.show(100);
//recursive calls
sierpinski(n-1, x0, y0, size / 2);
sierpinski(n-1, (x0 + x1) / 2, (y0 + y1) / 2, size / 2);
sierpinski(n-1, (x0 + x2) / 2, (y0 + y2) / 2, size / 2);
}
// read in a command-line argument n and plot an order Sierpinski Triangle
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
StdDraw.setPenRadius(0.005);
sierpinski(n);
}
}
Guy