Ejemplo n.º 1
0
  /** Returns distance between specified coordinates. */
  public static double getDistance(Point origin, Point destination) {
    int x = destination.getx() - origin.getx();
    int y = destination.gety() - origin.gety();
    double distance = Math.sqrt(x * x + y * y);

    return distance;
  }
Ejemplo n.º 2
0
  /** Returns angle in degrees from specified origin to specified destination. */
  public static int getAngle(Point origin, Point destination) // origin != destination
      {
    double distance = getDistance(origin, destination);
    int add = 0;
    int x = destination.getx() - origin.getx();
    int y = origin.gety() - destination.gety(); // Java flips things around

    double angleRad = Math.asin(Math.abs(y) / distance);
    double angleDeg = Math.toDegrees(angleRad);

    if ((x >= 0) && (y >= 0)) // Quadrant 1
    {
      angleDeg = angleDeg;
    }
    if ((x < 0) && (y > 0)) // Quadrant 2
    {
      angleDeg = 180 - angleDeg;
    }
    if ((x <= 0) && (y <= 0)) // Quadrant 3
    {
      angleDeg = 180 + angleDeg;
    }
    if ((x > 0) && (y < 0)) // Quadrant 4
    {
      angleDeg = 360 - angleDeg;
    }

    float angleFloat = Math.round(angleDeg);
    int angleInt = Math.round(angleFloat);

    return (angleInt);
  }
Ejemplo n.º 3
0
 boolean solve2() {
   int t = nextInt();
   if (t == 0) return false;
   int n = nextInt();
   Tower[] towers = new Tower[t];
   for (int i = 0; i < t; i++) {
     towers[i] = new Tower(new Point(nextInt(), nextInt()), nextInt());
   }
   n++;
   Point[] p = new Point[n];
   for (int i = 0; i < n; i++) {
     p[i] = new Point(nextInt(), nextInt());
   }
   // System.err.println(Arrays.toString(p));
   ArrayList<Point> points = new ArrayList<Point>();
   double r = 1;
   for (int i = 0; i < n - 1; i++) {
     double dist = p[i].dist(p[i + 1]) - (1 - r);
     int e = (int) Math.floor(dist + 1 - EPS);
     Point v = p[i + 1].subtract(p[i]).norm();
     for (int j = 0; j < e; j++) {
       points.add(p[i].add(v.multiply(j).add(v.multiply(1 - r))));
     }
     r = p[i + 1].dist(points.get(points.size() - 1));
   }
   if (p[n - 1].dist(points.get(points.size() - 1)) > 0.5 - EPS) {
     points.add(p[n - 1]);
   }
   // System.err.println(points);
   char last = 0;
   ArrayList<String> ans = new ArrayList<String>();
   for (int i = 0; i < points.size(); i++) {
     double maxP = Integer.MIN_VALUE;
     char here = 0;
     for (int j = 0; j < t; j++) {
       double w = towers[j].get(points.get(i));
       if (maxP < w - EPS) {
         maxP = w;
         here = (char) (j + 'A');
       }
     }
     if (here != last) {
       ans.add("(" + i + "," + here + ")");
     }
     last = here;
   }
   for (int i = 0; i < ans.size(); i++) {
     if (i != 0) out.print(" ");
     out.print(ans.get(i));
   }
   out.println();
   return true;
 }
Ejemplo n.º 4
0
 static void solve() {
   String[] s = in.next().split(",");
   Point p =
       reflect(
           new Line(
               Double.parseDouble(s[0]),
               Double.parseDouble(s[1]),
               Double.parseDouble(s[2]),
               Double.parseDouble(s[3])),
           new Point(Double.parseDouble(s[4]), Double.parseDouble(s[5])));
   out.println(p.getX() + " " + p.getY());
 }
Ejemplo n.º 5
0
  /** Dummy test method. */
  public static boolean dan(Point p1, Point p2) {
    //		Point p1 = mech1.getLocation();
    //		Point p2 = mech2.getLocation();
    int p1x = p1.getx();
    int p1y = p1.gety();
    int p2x = p2.getx();
    int p2y = p2.gety();
    int dx = p2x - p1x;
    int dy = p2y - p1y;
    int xdir = sign(dx);
    int ydir = sign(dy);
    //		double dist = Math.sqrt(dx*dx+dy*dy);
    double changex = (double) Math.abs(dx); // Diff in x
    double changey = (double) Math.abs(dy); // Diff in y

    double currentx = p1x;
    double currenty = p1y;
    int nextx = p1x;
    int nexty = p1y;
    double xdist, ydist;
    double xcost, ycost;

    int width = 5;

    while ((p2x - nextx) * xdir > 0 || (p2y - nexty) * ydir > 0) {
      xdist =
          dist(
              currentx, width,
              xdir); // total distance need to encounter next terrain object in x direction
      ydist = dist(currenty, width, ydir);

      xcost = (changex == 0) ? 10000 : xdist / changex;
      ycost = (changey == 0) ? 10000 : ydist / changey;

      if (xcost <= ycost) ydist = xdist * (changey / changex);
      else xdist = ydist * (changex / changey);

      currentx += xdir * xdist;
      currenty += ydir * ydist;

      nextx = (int) round(currentx, ydir);
      nexty = (int) round(currenty, ydir);

      //			if (Map.getNearestGrid(nextx,nexty).getType() !=0) return false;
    }

    System.out.println("Nextx: " + nextx + " Nexty: " + nexty);
    System.out.println("Currentx: " + currentx + " Currenty: " + currenty);
    return true;
  }
Ejemplo n.º 6
0
 double get(Point e) {
   return power / sqr(e.dist(p));
 }
Ejemplo n.º 7
0
 public String toString() {
   return "[(" + p1.getX() + "," + p1.getY() + "),(" + p2.getX() + "," + p2.getY() + ")]";
 }
Ejemplo n.º 8
0
 public Point getP2() {
   return p2.clone();
 }
Ejemplo n.º 9
0
 public Point getP1() {
   return p1.clone();
 }
Ejemplo n.º 10
0
 public double getX2() {
   return p2.getX();
 }
Ejemplo n.º 11
0
 public double getY1() {
   return p1.getY();
 }
Ejemplo n.º 12
0
 public double getX1() {
   return p1.getX();
 }
Ejemplo n.º 13
0
 public Line(Point p1, Point p2) {
   this.p1 = p1.clone();
   this.p2 = p2.clone();
 }
Ejemplo n.º 14
0
 public Vector plus(Point p) {
   return new Vector(this.x + p.getX(), this.y + p.getY());
 }
Ejemplo n.º 15
0
 public Vector(Point target, Point source) {
   this.x = target.getX() - source.getX();
   this.y = target.getY() - source.getY();
 }
Ejemplo n.º 16
0
 public double getY2() {
   return p2.getY();
 }
Ejemplo n.º 17
0
 boolean isConvex(Point a, Point b, Point c) {
   return det(b.subtract(a), c.subtract(b)) >= 0;
 }