CS 145 Lecture 14 – Testing and conditionals via collision detection
October 28, 2011 by Chris Johnson. Filed under
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378
Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379
Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380
Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381
Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382
Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378
Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379
Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380
Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381
Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382
Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378
Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379
Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380
Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381
Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382
Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
cs145, fall 2011, lectures.
Agenda
- lab switcharoo
- collision detection
- test-driven development
Collision detection
Suppose you have two horizontal line segments in the same line. (Or two pens.) When are they intersecting?
Suppose you have two circles. When are they intersecting?
Suppose you have two rectangles. When are they intersecting?
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);
}
}
Test-driven development
Here’s by far the best strategy for writing a method with minimal headache:
- Write its specification. What does it need to do? What does it needs from the outside world? What does it give back the outside world?
- Write its signature/interface.
- Write tests.
- Implement it.
- Execute your tests.