翻译坐标方法java

时间:2014-06-10 00:51:03

标签: java methods

我似乎无法使我的转换方法正确。关于如何使该方法更好地转化这一点的任何建议?即,当调用此方法时,它应该能够给出新点。我也有问题创建斜率方法...我知道斜率是y2-y1 / x2-x1但是我如何将其变成方法。我可以为斜坡导入Math类吗?非常感谢

       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;
           }
       }

1 个答案:

答案 0 :(得分:0)

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;
}