Skip to content

Instantly share code, notes, and snippets.

@sk89q
Created June 9, 2009 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sk89q/59c08fdf1de2a346d4fb to your computer and use it in GitHub Desktop.
Save sk89q/59c08fdf1de2a346d4fb to your computer and use it in GitHub Desktop.
<?php
function isNear($c1, $c2)
{
$dist = sqrt(pow($c2[0] - $c1[0], 2) +
pow($c2[1] - $c1[1], 2));
if ($dist < .001) {
return true;
}
return false;
}
function getTranslatedCoord($x, $y, $about_x, $about_y, $angle)
{
$x -= $about_x;
$y -= $about_y;
return array($x * cos($angle) - $y * sin($angle) + $about_x,
$x * sin($angle) + $y * cos($angle) + $about_y);
}
function getPossibleCoords($object)
{
$type = $object->getType();
$x = $object->getX();
$y = $object->getY();
$a = $object->getAngleRad();
$w = $object->getWidth();
$h = $object->getHeight();
switch ($type) {
case FCObject::GOAL_RECT:
return array(
getTranslatedCoord($x + $w/2, $y + $h/2, $x, $y, $a),
getTranslatedCoord($x + $w/2, $y - $h/2, $x, $y, $a),
getTranslatedCoord($x - $w/2, $y + $h/2, $x, $y, $a),
getTranslatedCoord($x - $w/2, $y - $h/2, $x, $y, $a),
);
case FCObject::GOAL_CIRCLE:
case FCObject::UNPOWERED_WHEEL:
case FCObject::CW_WHEEL:
case FCObject::CCW_WHEEL:
return array(
array($x,
$y),
array($x+cos($a+M_PI/2)*$w/2,
$y+sin($a+M_PI/2)*$w/2),
array($x+cos($a+M_PI/2)*-$w/2,
$y+sin($a+M_PI/2)*-$w/2),
array($x+cos($a)*$w/2,
$y+sin($a)*$w/2),
array($x+cos($a)*-$w/2,
$y+sin($a)*-$w/2),
);
case FCObject::WATER_ROD:
case FCObject::WOOD_ROD:
return array(
array($x+cos($a)*$w/2,
$y+sin($a)*$w/2),
array($x+cos($a)*-$w/2,
$y+sin($a)*-$w/2),
);
}
return array();
}
function countUniqueJoints($stage)
{
$joint_count = 0;
$objects_lookup = array();
$joints = array();
$connections = array();
foreach ($stage->getObjects() as $object) {
$joints[$object->getId()] = array();
$objects_lookup[$object->getId()] = $object;
}
foreach ($stage->getObjects() as $object) {
foreach ($object->getJointsList() as $j) {
$joints[$object->getId()][] = $j;
$joints[$j][] = $object->getId();
}
}
foreach ($stage->getObjects() as $object) {
$id = $object->getId();
foreach ($joints[$id] as $joint_id) {
if (!isset($objects_lookup[$joint_id])) {
continue;
}
$target_object = $objects_lookup[$joint_id];
$possible_coords = getPossibleCoords($object);
$possible_target_coords = getPossibleCoords($target_object);
$coord = null;
foreach ($possible_coords as $c1) {
foreach ($possible_target_coords as $c2) {
if (isNear($c1, $c2)) {
$coord = $c1;
}
}
}
if ($coord) {
$found = false;
foreach ($connections as &$conn) {
if (isNear($conn[0], $coord)) {
$found = true;
if (in_array($id, $conn[1]) ||
in_array($joint_id, $conn[1])) {
} else {
$joint_count++;
}
if (!in_array($id, $conn[1])) {
$conn[1][] = $id;
}
if (!in_array($joint_id, $conn[1])) {
$conn[1][] = $joint_id;
}
break;
}
}
if (!$found) {
$connections[] = array(
$coord, array($id, $joint_id),
);
$joint_count++;
}
}
}
}
return $joint_count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment