예제 #1
0
 @Override
 public void activate(Sprite sprite) {
   Vector component = sprite.getVelocity().getComponentVector(myDirection);
   component.negate();
   sprite.addVector(component);
   sprite.addVector(new Vector(myDirection, mySpeed));
 }
예제 #2
0
 public Vector add(Vector addVector) {
   // basically, we can do this graphically
   // move the addVector so that it starts at our end
   // then make a vector from our start to the end of the other vector
   Vector movedVect = addVector.moveTo(this.getP2());
   return new Vector(this.getP1(), movedVect.getP2());
 }
예제 #3
0
  private void directory_to_directory(
      String source_base_name, String destination_base_name, String directory_name)
      throws IOException, InterruptedException {
    // E.g.
    // source_base_name:        /foo/bar
    // destination_base_name:   /x/y
    // directory_name:     abc
    //
    // - Create /x/y/abc
    // - Copy /foo/bar/abc/* to /x/y/abc/*

    // Get source directory.
    String source_directory_name = Path.concatenate(source_base_name, directory_name);
    File source_directory = File.create(source_directory_name);
    Assertion.check(source_directory.isDirectory());

    // Get destination directory, creating it if
    // necessary.
    String destination_directory_name = Path.concatenate(destination_base_name, directory_name);
    File destination_directory = File.create(destination_directory_name);
    destination_directory.mkdirs();

    // Get files in source directory
    String[] source_file_names = source_directory.list();
    Vector source_files = new Vector();
    for (int i = 0; i < source_file_names.length; i++)
      source_files.addElement(File.create(source_directory_name, source_file_names[i]));

    // Copy
    to_directory(source_files, destination_directory);
  }
예제 #4
0
  /**
   * get the angle between this and other
   *
   * @param other
   * @return
   */
  public double getAngleBetween(Vector other) {
    // make sure this and other share a point
    // and that point is P1
    if (!this.getP1().equals(other.getP1())) {
      // see if they share any point
      if (other.getP2().equals(this.getP1())) {
        // reverse other
        other = other.reverse();
      } else {
        throw new IllegalArgumentException("Vectors do not meet up");
      }
    }

    double dotProduct = this.dot(other);
    double thisMag = this.getMagnitude();
    double otherMag = other.getMagnitude();

    return Math.acos(dotProduct / (thisMag * otherMag));

    //		Vector thisUnit = this.getUnitVector();
    //		Vector otherUnit = other.getUnitVector();

    //		return Math.atan2(otherUnit.getY2(), otherUnit.getX2()) - Math.atan2(thisUnit.getY2(),
    // thisUnit.getX2());
    //		return Math.acos(arg0)
  }
예제 #5
0
  protected Colour shadeHit(Ray ray, Intersection inter) {
    Colour colourD = inter.getBestHitMaterial().diffuse;
    Colour colourC = inter.getBestHitMaterial().ambient;
    float R = 0;
    float G = 0;
    float B = 0;

    for (Light light : scene.getLights()) {
      Vector norms = light.pos.subtract(inter.getBestHitPoint());
      norms.normalize();
      Vector normm = inter.getBestHitNormal();
      float t = norms.dot(normm);
      if (t > 0) {
        R += ((colourD.r * light.colour.r * t) + (colourC.r * light.colour.r));
        G += ((colourD.g * light.colour.g * t) + (colourC.g * light.colour.g));
        B += ((colourD.b * light.colour.b * t) + (colourC.b * light.colour.b));
      } else {
        R += (colourC.r * light.colour.r);
        G += (colourC.g * light.colour.g);
        B += (colourC.b * light.colour.b);
      }
    }

    return new Colour(R, G, B);
  }
예제 #6
0
 @Override
 public Vector getForceOnMass(Mass m) {
   if (!getToggle()) return new Vector();
   Vector result = new Vector(m.getVelocity());
   double viscosityScale = getProperty(Keywords.SCALE);
   result.setMagnitude(result.getMagnitude() * viscosityScale);
   result.turn(Keywords.DEGREES_TURN_AROUND);
   return result;
 }
예제 #7
0
 public Vector getPerpendicularVectorPointedTowards(
     Point2D startPoint, double magnitude, Point2D directionPoint) {
   Vector perpVect = this.moveTo(startPoint);
   if (perpVect.relativeCCW(directionPoint) > 0) {
     // we need to turn counter clockwise
     perpVect = perpVect.rotate(-1 * Math.PI / 2.0);
   } else {
     // we'll rotate clockwise
     perpVect = perpVect.rotate(Math.PI / 2.0);
   }
   return perpVect.rescale(magnitude);
 }
예제 #8
0
파일: Turtle.java 프로젝트: jwang93/SLogo
  /**
   * Recursive helper for move. Uses its own helper methods, overrunsTop, Bottom, Right, and Left to
   * calculate next move. Calls itself recursively when turtle hits the edge of the screen
   * (overruns).
   *
   * @param pixels changes every time with recursive call until it is less than 0 (because of
   *     possible rounding errors with trig functions)
   */
  protected void moveRecursiveHelper(double pixels) {
    // Checkstyle and clean-up rules conflict on brackets - this is a recurring problem
    // throughout our code
    if (pixels <= 0 + PRECISION_LEVEL) return;

    Location currentLocation = getLocation();
    Location nextLocation = getLocation();
    Location nextCenter = nextLocation;
    nextLocation.translate(new Vector(getHeading(), pixels));
    Location[] replacements = {nextLocation, nextCenter};

    if (nextLocation.getY() < 0) {
      // top
      replacements = overrunsTop();
    } else if (nextLocation.getY() > myCanvasBounds.getHeight()) {
      // bottom
      replacements = overrunBottom();
    } else if (nextLocation.getX() > myCanvasBounds.getWidth()) {
      // right
      replacements = overrunRight();
    } else if (nextLocation.getX() < 0) {
      // left
      replacements = overrunLeft();
    }

    nextLocation = replacements[0];
    nextCenter = replacements[1];

    setCenter(nextCenter);
    double newPixels = pixels - (Vector.distanceBetween(currentLocation, nextLocation));
    if (myPenDown) {
      myLine.addLineSegment(currentLocation, nextLocation);
    }
    moveRecursiveHelper(newPixels);
  }
예제 #9
0
  /**
   * ******************************************************************************* addMarker
   *
   * <p>This method is responsible for adding crumbs to the handler and determining when crumbs
   * should become invisible. Right now, crumbs are removed once there are more than 20 markers in
   * the handler. Crumbs are removed according to when they were placed on the manifold by the user.
   * *******************************************************************************
   */
  public void addMarker(ManifoldPosition mp) {
    ManifoldPosition pos = new ManifoldPosition(mp);
    Vector vec = new Vector(pos.getDirectionForward());
    vec.scale(-.25);
    pos.move(vec);
    Marker m = new Marker(pos, crumb, Marker.MarkerType.FIXED);
    trail.add(m);
    markers.addMarker(m);
    counter++;

    int numMarkers = trail.size() - 1;
    if (numMarkers >= 5) {
      Marker toRemove = trail.poll();
      toRemove.flagForRemoval();
    }
  }
예제 #10
0
파일: Turtle.java 프로젝트: jwang93/SLogo
 /**
  * Moves turtle to location **AS IN VIEW'S COORDINATES**
  *
  * @param location to move to
  * @return distance of move
  */
 protected int setLocation(Location location) {
   double heading = getHeading();
   towards(location);
   int distance =
       (int) Vector.distanceBetween(getLocation(), convertFromViewCoordinates(location));
   move(distance);
   setHeading(heading);
   return distance;
 }
예제 #11
0
  // check for move out of bounds
  private Vector getBounce(Dimension bounds) {
    final double IMPULSE_MAGNITUDE = 2;
    int tolerance = 10;

    Vector impulse = new Vector();
    if (getLeft() < tolerance) {
      impulse = new Vector(RIGHT_DIRECTION, IMPULSE_MAGNITUDE);
    } else if (getRight() - tolerance > bounds.width) {
      impulse = new Vector(LEFT_DIRECTION, IMPULSE_MAGNITUDE);
    }
    if (getTop() < tolerance) {
      impulse = new Vector(DOWN_DIRECTION, IMPULSE_MAGNITUDE);
    } else if (getBottom() - tolerance > bounds.height) {
      impulse = new Vector(UP_DIRECTION, IMPULSE_MAGNITUDE);
    }
    impulse.scale(getMyMass());
    impulse.scale(getVelocity().getRelativeMagnitude(impulse));
    return impulse;
  }
예제 #12
0
  @Override
  protected Colour shadeHit(Ray ray, Intersection inter) {
    Colour colourD = inter.getBestHitMaterial().diffuse;
    Colour colourC = inter.getBestHitMaterial().ambient;
    float R = 0;
    float G = 0;
    float B = 0;
    Point start = inter.getBestHitPoint();
    Vector temp = new Vector(inter.getBestHitNormal());
    start.add(temp);
    Ray shadowfeeler = new Ray(start);

    for (Light light : scene.getLights()) {
      R += (colourC.r * light.colour.r);
      G += (colourC.g * light.colour.g);
      B += (colourC.b * light.colour.b);

      Vector dir = light.pos.subtract(start);
      shadowfeeler.dir = dir;

      if (!isInShadow(shadowfeeler)) {
        Vector norms = light.pos.subtract(inter.getBestHitPoint());
        norms.normalize();
        Vector normm = inter.getBestHitNormal();
        float t = norms.dot(normm);
        if (t > 0) {
          R += (colourD.r * light.colour.r * t);
          G += (colourD.g * light.colour.g * t);
          B += (colourD.b * light.colour.b * t);
        }
      }
    }
    if (this.recursionDepth <= this.maxRecursionDepth
        && inter.getBestHitMaterial().reflectivity >= 0.1) {
      Ray reflRay = this.computeReflectedRay(ray, inter); // Maybe change to start later on
      Colour col = this.shade(reflRay);
      R += col.r * inter.getBestHitMaterial().reflectivity;
      G += col.g * inter.getBestHitMaterial().reflectivity;
      B += col.b * inter.getBestHitMaterial().reflectivity;
    }

    return new Colour(R, G, B);
  }
예제 #13
0
 private void process_args(String[] args) {
   Vector source_names = new Vector();
   for (int i = 0; i < args.length; i++) {
     String arg = args[i];
     if (isFlag(arg)) process_flag(arg);
     else source_names.addElement(arg);
   }
   _destination_name = (String) source_names.lastElement();
   int n_sources = source_names.size() - 1;
   if (n_sources == 0)
     throw new NakshException(
         "The arguments to cp must include a "
             + "destination file or directory, and at "
             + "least one source file or directory.");
   _sources = new Vector(n_sources);
   for (int i = 0; i < n_sources; i++) {
     String source_name = (String) source_names.elementAt(i);
     _sources.addElement(File.create(source_name));
   }
 }
예제 #14
0
  /** XXX. */
  @Override
  public void update(double elapsedTime, Dimension bounds) {
    System.out.println("1");
    Vector bounce = getBounce(bounds);
    applyForce(bounce);

    myEnvironment.applyEnvironment(this, bounds);

    // add gravity
    if (bounce.getYChange() == 0) {
      myEnvironment.applyGravity(this);
    }

    // convert force back into Mover's velocity
    getVelocity().sum(myAcceleration);
    myAcceleration.reset();
    // move mass by velocity
    // detect position

    super.update(elapsedTime, bounds);
  }
예제 #15
0
 private void to_directory(Vector sources, File destination_directory)
     throws IOException, InterruptedException {
   Assertion.check(destination_directory.exists());
   for (Enumeration source_scan = sources.elements(); source_scan.hasMoreElements(); ) {
     File source = (File) source_scan.nextElement();
     if (source.isFile()) {
       File destination = File.create(destination_directory.getPath(), source.getName());
       file_to_file(source, destination);
     } else if (_recursive) {
       String source_base = source.getParent();
       if (source_base == null) source_base = ".";
       String destination_base = destination_directory.getCanonicalPath();
       directory_to_directory(source_base, destination_base, source.getName());
     }
   }
 }
예제 #16
0
 private Ray computeReflectedRay(Ray ray, Intersection best) {
   Vector m = best.getBestHitNormal();
   Vector d = ray.dir;
   Vector r = d.substract(m.multV(d.dot(m) * 2));
   return new Ray(best.getBestHitPoint(), r);
 }
예제 #17
0
 public Vector getPerpendicularVector(Point2D startPoint, double magnitude) {
   Vector perpVect = this.moveTo(startPoint);
   perpVect = perpVect.rotate(Math.PI / 2.0);
   return perpVect.rescale(magnitude);
 }
예제 #18
0
 public Vector getParallelVector(Point2D startPoint, double magnitude) {
   Vector parallelVector = this.moveTo(startPoint);
   return parallelVector.rescale(magnitude);
 }
예제 #19
0
 /* get the scalar projection of this on other
  * i.e. get the magnitude of the component of the this vector in the direction of the other vector
  */
 public double scalerProjectionOnto(Vector other) {
   return this.dot(other.getUnitVector());
 }
예제 #20
0
 public double dot(Vector other) {
   return (this.getXMag() * other.getXMag()) + (this.getYMag() * other.getYMag());
 }
 public void applyGravity(Mass m) {
   Vector g = new Vector(getMyGravity());
   g.scale(m.getMyMass());
   m.applyForce(g);
 }
 public void applyVescosity(Mass m) {
   Vector vescoForce = new Vector(m.getVelocity());
   vescoForce.scale(myViscosity);
   vescoForce.negate();
   m.applyForce(vescoForce);
 }
예제 #23
0
파일: Turtle.java 프로젝트: jwang93/SLogo
 /**
  * Sets heading to go towards location **AS IN VIEW'S COORDINATES**
  *
  * @param location location to set heading towards
  * @return distance of turn
  */
 protected double towards(Location location) {
   double currentHeading = getHeading();
   Vector tvector = new Vector(getLocation(), convertFromViewCoordinates(location));
   setHeading(tvector.getDirection());
   return Math.abs(currentHeading - getHeading());
 }
예제 #24
0
  protected void writeAllNativeTables(ClassClass classes[]) {
    sortClasses(classes);
    Vector nativeClasses = new Vector();

    // (1) Write all the function declarations
    for (int i = 0; i < classes.length; i++) {
      boolean classHasNatives = false;
      EVMClass cc = (EVMClass) classes[i];
      if (cc.isPrimitiveClass() || cc.isArrayClass()) {
        continue;
      }
      EVMMethodInfo m[] = cc.methods;
      sortMethods(m);
      int nmethod = (m == null) ? 0 : m.length;
      for (int j = 0; j < nmethod; j++) {
        EVMMethodInfo meth = m[j];
        MethodInfo mi = meth.method;
        if (!isNativeOrEntryFunc(cc, mi)) {
          continue;
        }
        if (!classHasNatives) {
          classHasNatives = true;
          nativeClasses.addElement(cc);
        }
        int entry;
        entry = checkEntry(cc, mi);
        if (entry < 0 || use_entries[entry].length > 4) {
          // This is a "native"
          out.println(
              "extern \"C\" " + mi.getJNIReturnType() + " " + mi.getNativeName(true) + "();");
        }
        if (entry >= 0) {
          // This is an "entry" (could also be a "native")
          out.println("extern \"C\" void " + use_entries[entry][3] + "();");
        }
      }
    }
    out.println();
    out.println();

    // (2) Write all the "_natives[]" and "_entries[]" tables for
    //     individual classes

    for (Enumeration e = nativeClasses.elements(); e.hasMoreElements(); ) {
      EVMClass cc = (EVMClass) e.nextElement();
      EVMMethodInfo m[] = cc.methods;
      int nmethod = (m == null) ? 0 : m.length;
      int num_native_methods = 0;
      int num_entry_methods = 0;

      for (int j = 0; j < nmethod; j++) {
        EVMMethodInfo meth = m[j];
        MethodInfo mi = meth.method;
        if (!isNativeOrEntryFunc(cc, mi)) {
          continue;
        }
        if (isNativeFunc(cc, mi)) {
          num_native_methods++;
        }
        if (isEntryFunc(cc, mi)) {
          num_entry_methods++;
        }
      }

      // Write the "_natives[]" struct
      if (num_native_methods > 0) {
        out.println("static const JvmNativeFunction " + cc.getNativeName() + "_natives[] = " + "{");
        for (int j = 0; j < nmethod; j++) {
          EVMMethodInfo meth = m[j];
          MethodInfo mi = meth.method;
          if (!isNativeOrEntryFunc(cc, mi)) {
            continue;
          }
          if (isNativeFunc(cc, mi)) {
            out.print(pad("  JVM_NATIVE(\"" + mi.name.string + "\",", 30));
            out.print(pad("\"" + mi.type.string + "\", ", 25));
            out.println(mi.getNativeName(true) + "),");
          }
        }
        out.println("  {(char*)0, (char*)0, (void*)0}");
        out.println("};");
        out.println();
      }

      // Write the "_entries[]" struct
      if (num_entry_methods > 0) {
        out.println("static const JvmNativeFunction " + cc.getNativeName() + "_entries[] = " + "{");
        for (int j = 0; j < nmethod; j++) {
          EVMMethodInfo meth = m[j];
          MethodInfo mi = meth.method;
          if (!isNativeOrEntryFunc(cc, mi)) {
            continue;
          }
          int entry;
          if ((entry = checkEntry(cc, mi)) >= 0) {
            out.print(
                "  JVM_ENTRY(\"" + mi.name.string + "\"," + spaces(20 - mi.name.string.length()));
            out.print("\"" + mi.type.string + "\", ");
            out.println(use_entries[entry][3] + "),");
          }
        }
        out.println("  {(char*)0, (char*)0, (void*)0}");
        out.println("};");
        out.println();
      }
    }

    // (3) Write the top-level table

    out.println("const JvmNativesTable jvm_natives_table[] = {");

    for (Enumeration e = nativeClasses.elements(); e.hasMoreElements(); ) {
      EVMClass cc = (EVMClass) e.nextElement();
      String className = cc.ci.className;
      out.println("  JVM_TABLE(\"" + className + "\",");
      EVMMethodInfo m[] = cc.methods;
      int nmethod = (m == null) ? 0 : m.length;

      int num_native_methods = 0;
      int num_entry_methods = 0;

      for (int j = 0; j < nmethod; j++) {
        EVMMethodInfo meth = m[j];
        MethodInfo mi = meth.method;
        if (!isNativeOrEntryFunc(cc, mi)) {
          continue;
        }
        if (isNativeFunc(cc, mi)) {
          num_native_methods++;
        }
        if (isEntryFunc(cc, mi)) {
          num_entry_methods++;
        }
      }

      if (num_native_methods > 0) {
        out.println(spaces(40) + cc.getNativeName() + "_natives,");
      } else {
        out.println(spaces(40) + "(JvmNativeFunction*)0,");
      }
      if (num_entry_methods > 0) {
        out.println(spaces(40) + cc.getNativeName() + "_entries),");
      } else {
        out.println(spaces(40) + "(JvmNativeFunction*)0),");
      }
    }
    out.println("  JVM_TABLE((char*)0, (JvmNativeFunction*)0, " + "(JvmNativeFunction*)0)");
    out.println("};");

    // (4) Write the jvm_native_execution_top-level table

    out.println("const JvmExecutionEntry jvm_api_entries[] = {");
    for (Enumeration e = nativeClasses.elements(); e.hasMoreElements(); ) {
      EVMClass cc = (EVMClass) e.nextElement();
      EVMMethodInfo m[] = cc.methods;
      int nmethod = (m == null) ? 0 : m.length;

      for (int j = 0; j < nmethod; j++) {
        EVMMethodInfo meth = m[j];
        MethodInfo mi = meth.method;
        int index;
        if ((index = checkEntry(cc, mi)) >= 0) {
          String entryName = use_entries[index][3];
          out.println("{(unsigned char*)&" + entryName + ",");
          out.println("(char*)\"" + entryName + "\"},");
        }
      }
    }
    out.println("{(unsigned char*)0, (char*)0}};");
  }
예제 #25
0
 /** Use the given force to change this mass's acceleration. */
 public void applyForce(Vector force) {
   Vector scaled = new Vector(force);
   scaled.scale(1 / myMass);
   myAcceleration.sum(scaled);
 }