teaching machines

CS 145 Lecture 15 – More colliding

October 31, 2011 by . Filed under cs145, fall 2011, lectures.

Agenda

Code

CollisionUtilities.java

package lecture;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class CollisionUtilities {

  public static void main(String[] args) throws FileNotFoundException {
    Scanner in = new Scanner(new File("/home/cjohnson/Desktop/lines.txt"));

    while (in.hasNextDouble()) {
      double leftA = in.nextDouble();
      double rightA = in.nextDouble();
      double leftB = in.nextDouble();
      double rightB = in.nextDouble();
      boolean expected = in.nextBoolean();

      boolean actual = isColliding(leftA, rightA, leftB, rightB);
      if (actual != expected) {
        System.out.printf("[%f, %f] vs. [%f, %f] expected: " + expected + ", actual: " + actual + "%n", leftA, rightA, leftB, rightB);
      }
    }

    in.close();
  }

  /**
   * Checks whether two 1-D line segments intersect.
   * 
   * @param leftA
   * Left bounds of line segment A, assumed < rightA
   * @param rightA
   * Right bounds of line segment A
   * @param leftB
   * Left bounds of line segment B, assumed < rightB
   * @param rightB
   * Right bounds of line segment B
   * @return True if they collide, false otherwise
   */
  public static boolean isColliding(double leftA,
                                    double rightA,
                                    double leftB,
                                    double rightB) {
    return !(leftB > rightA || rightB < leftA);
  }

  /**
   * Checks whether two circles intersect.
   * @param xA X-coordinate of circle A's center
   * @param yA Y-coordinate of circle A's center
   * @param radiusA Radius of circle A
   * @param xB X-coordinate of circle B's center
   * @param yB Y-coordinate of circle B's center
   * @param radiusB Radius of circle B
   * @returns True if colliding, false otherwise
   */
  public static boolean isColliding(double xA,
                                    double yA,
                                    double radiusA,
                                    double xB,
                                    double yB,
                                    double radiusB) {
    double distance = Math.sqrt(Math.pow(yA - yB, 2) + Math.pow(xA - xB, 2));
    return distance <= radiusA + radiusB; 
  }
}

ColliderPanel.java

package lecture;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ColliderPanel extends JPanel implements MouseMotionListener {
  public static void main(String[] args) {
    JFrame frame = new JFrame("Collision");
    frame.add(new ColliderPanel());
    frame.setSize(512, 512);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }

  private int leftA = 200;
  private int rightA = 300;

  private int leftB;
  private int rightB;

  private int xA = 200;
  private int yA = 300;
  private int radiusA = 75;

  private int xB;
  private int yB;
  private int radiusB = 50;

  /**
   * Creates a new canvas for testing collision detection. It tracks its own
   * mouse movement.
   */
  public ColliderPanel() {
    addMouseMotionListener(this);
  }

  /**
   * Draws the canvas. TODO: have it draw in red when the shapes collide.
   * 
   * @param g
   * The drawing canvas
   */
  public void paintComponent(Graphics g) {
    g.clearRect(0, 0, getWidth(), getHeight());

    // if colliding, color chartreuse
    // // otherwise black

    // if (CollisionUtilities.isColliding(leftA, rightA, leftB, rightB)) {
    // // g.setColor(new Color(236, 235, 189));
    // g.setColor(Color.PINK);
    // } else {
    // g.setColor(Color.BLACK);
    // }
    //
    // g.drawLine(leftA, 300, rightA, 300);
    // g.drawLine(leftB, 300, rightB, 300);

    //
    if (CollisionUtilities.isColliding(xA, yA, radiusA, xB, yB, radiusB)) {
      g.setColor(new Color(236, 235, 189));
      g.setColor(Color.PINK);
    } else {
      g.setColor(Color.BLACK);
    }
    //
    g.fillOval(xA - radiusA, yA - radiusA, radiusA * 2, radiusA * 2);
    g.fillOval(xB - radiusB, yB - radiusB, radiusB * 2, radiusB * 2);
  }

  /**
   * Handles mouse drag (i.e., a button is down) by ignoring them.
   * 
   * @param event
   * Mouse drag event
   */
  public void mouseDragged(MouseEvent event) {
  }

  /**
   * Handles mouse motion when no button is down. We simply update the current
   * mouse position and redraw the canvas so that colliding object is drawn in
   * the new position.
   * 
   * @param event
   * Mouse motion event
   */
  public void mouseMoved(MouseEvent event) {
    leftB = event.getX() - 50;
    rightB = leftB + 100;

    xB = event.getX();
    yB = event.getY();

    repaint();
  }
}

TODO

Haiku

When I code, I’m king
Booleans express my will
Scanner, watch yourself