Skip to content

Commit

Permalink
finished product
Browse files Browse the repository at this point in the history
  • Loading branch information
kballenegger committed Jan 31, 2011
1 parent f0f0036 commit f65aee8
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 80 deletions.
30 changes: 30 additions & 0 deletions _prompt.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Van De Graaf Generator, by Kenneth Ballenegger</title>
<style type="text/css" media="screen">
#container {
width: 200px;
height: 200px;
margin: auto;
margin-top: 200px;
}
</style>
</head>

<body>

<div id="container">
<?if(!empty($message)):echo $message;endif;?>
<form action="index.php" method="post" accept-charset="utf-8">
<p>Width: <input type="text" name="width" value="0" id="width"></p>
<p>Height: <input type="text" name="height" value="0" id="height"></p>

<p><input type="submit" value="Continue &rarr;"></p>
</form>
</div>

</body>
</html>
28 changes: 28 additions & 0 deletions _response.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Van De Graaf Generator, by Kenneth Ballenegger</title>
<style type="text/css" media="screen">
#container {
width: 200px;
height: 200px;
margin: auto;
margin-top: 200px;
}
</style>
</head>

<body>

<div id="container">
<p>Here's your margins, buddy!</p>
<p>Top: <?=$top?></p>
<p>Bottom: <?=$bottom?></p>
<p>Inside: <?=$inside?></p>
<p>Outside: <?=$outside?></p>
</div>

</body>
</html>
62 changes: 62 additions & 0 deletions attemplate.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

//
// attemplate.inc.php
//
// Created by Kenneth Ballenegger on 2009-06-30.
// Copyright (c) 2009 Arure Talon. All rights reserved.
//

/*
*
* ATTemplate
*
* A template wrapper, based on the simplicity of using PHP itself for templating.
*
*/

class ATTemplate
{
private $proprieties;

// constructor
// optional $props: initial proprieties array
function ATTemplate($props=null)
{
if(isset($props))
if(is_array($props))
$this->proprieties = $props;
else
$this->proprieties = array();
}

// set $key propriety as $value
function set($key, $value)
{
$this->proprieties[$key] = $value;
}

// push array contents into proprieties
// $props: proprieties array
function push($props)
{
if(is_array($props))
foreach($props as $key => $value)
$this->set($key, $value);
}

// parse template $file
// returns parsed text
function parse($file)
{
$unique_id = uniqid();
$file_{$unique_id} = $file; // this is to protect us from the eventuality the user has a propriety named file
foreach($this->proprieties as $key => $value)
${$key} = $value;
ob_start();
require $file_{$unique_id};
return ob_get_clean();
}
}

?>
98 changes: 18 additions & 80 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,83 +1,21 @@
<?php

function point($x, $y) {
return array(0=>$x, 1=>$y);
require_once 'attemplate.inc.php';
require_once 'vdg.php';

if (empty($_POST['width']) || empty($_POST['height'])
|| !is_numeric($_POST['width']) || !is_numeric($_POST['height'])) {
$template = new ATTemplate(array());
echo $template->parse('_prompt.tpl');
} else {
$width = $_POST['width'];
$height = $_POST['height'];
list($top, $bottom, $inside, $outside) = van_der_graaf($width, $height);
$template = new ATTemplate(array(
'top' => $top,
'bottom' => $bottom,
'inside' => $inside,
'outside' => $outside
));
echo $template->parse('_response.tpl');
}

function intersect($A, $B, $C, $D) {
$Ax = $A[0]; $Ay = $A[1];
$Bx = $B[0]; $By = $B[1];
$Cx = $C[0]; $Cy = $C[1];
$Dx = $D[0]; $Dy = $D[1];

// Fail if either line is undefined.
if ($Ax==$Bx && $Ay==$By || $Cx==$Dx && $Cy==$Dy) return NO;

// (1) Translate the system so that point A is on the origin.
$Bx-=$Ax; $By-=$Ay;
$Cx-=$Ax; $Cy-=$Ay;
$Dx-=$Ax; $Dy-=$Ay;

// Discover the length of segment A-B.
$distAB=sqrt($Bx*$Bx+$By*$By);

// (2) Rotate the system so that point B is on the positive X axis.
$theCos=$Bx/$distAB;
$theSin=$By/$distAB;
$newX=$Cx*$theCos+$Cy*$theSin;
$Cy =$Cy*$theCos-$Cx*$theSin; $Cx=$newX;
$newX=$Dx*$theCos+$Dy*$theSin;
$Dy =$Dy*$theCos-$Dx*$theSin; $Dx=$newX;

// Fail if the lines are parallel.
if ($Cy==$Dy) return NO;

// (3) Discover the position of the intersection point along line A-B.
$ABpos=$Dx+($Cx-$Dx)*$Dy/($Dy-$Cy);

// (4) Apply the discovered position to line A-B in the original coordinate system.
$X=$Ax+$ABpos*$theCos;
$Y=$Ay+$ABpos*$theSin;

return(point($X, $Y));
}

// Returns array($top, $bottom, $inside, $outside)
function van_der_graaf($width, $height) {
$T = intersect(
point(0-$width, $height),
point($width, 0),
point(0, 0),
point($width, $height)
);

$K = intersect(
point(0, 0),
point($width, $height),
point(0-$T[0], $T[1]),
point($T[0], 0)
);

$O = intersect(
$K,
point($width, $K[1]),
point(0-$width, $height),
point($width, 0)
);

$B = intersect(
point(0, 0),
point($width, $height),
$O,
point($O[0], $height)
);

$top = $K[1];
$bottom = $height-$B[1];
$inside = $K[0];
$outside = $width-$B[0];
return array($top, $bottom, $inside, $outside);
}


$M = van_der_graaf(7, 10);
80 changes: 80 additions & 0 deletions vdg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

function point($x, $y) {
return array(0=>$x, 1=>$y);
}

function intersect($A, $B, $C, $D) {
$Ax = $A[0]; $Ay = $A[1];
$Bx = $B[0]; $By = $B[1];
$Cx = $C[0]; $Cy = $C[1];
$Dx = $D[0]; $Dy = $D[1];

// Fail if either line is undefined.
if ($Ax==$Bx && $Ay==$By || $Cx==$Dx && $Cy==$Dy) return NO;

// (1) Translate the system so that point A is on the origin.
$Bx-=$Ax; $By-=$Ay;
$Cx-=$Ax; $Cy-=$Ay;
$Dx-=$Ax; $Dy-=$Ay;

// Discover the length of segment A-B.
$distAB=sqrt($Bx*$Bx+$By*$By);

// (2) Rotate the system so that point B is on the positive X axis.
$theCos=$Bx/$distAB;
$theSin=$By/$distAB;
$newX=$Cx*$theCos+$Cy*$theSin;
$Cy =$Cy*$theCos-$Cx*$theSin; $Cx=$newX;
$newX=$Dx*$theCos+$Dy*$theSin;
$Dy =$Dy*$theCos-$Dx*$theSin; $Dx=$newX;

// Fail if the lines are parallel.
if ($Cy==$Dy) return NO;

// (3) Discover the position of the intersection point along line A-B.
$ABpos=$Dx+($Cx-$Dx)*$Dy/($Dy-$Cy);

// (4) Apply the discovered position to line A-B in the original coordinate system.
$X=$Ax+$ABpos*$theCos;
$Y=$Ay+$ABpos*$theSin;

return(point($X, $Y));
}

// Returns array($top, $bottom, $inside, $outside)
function van_der_graaf($width, $height) {
$T = intersect(
point(0-$width, $height),
point($width, 0),
point(0, 0),
point($width, $height)
);

$K = intersect(
point(0, 0),
point($width, $height),
point(0-$T[0], $T[1]),
point($T[0], 0)
);

$O = intersect(
$K,
point($width, $K[1]),
point(0-$width, $height),
point($width, 0)
);

$B = intersect(
point(0, 0),
point($width, $height),
$O,
point($O[0], $height)
);

$top = $K[1];
$bottom = $height-$B[1];
$inside = $K[0];
$outside = $width-$B[0];
return array($top, $bottom, $inside, $outside);
}

0 comments on commit f65aee8

Please sign in to comment.