Skip to content

Instantly share code, notes, and snippets.

@mathdoodle
Last active August 29, 2015 14:22
Show Gist options
  • Save mathdoodle/04267588af5347959827 to your computer and use it in GitHub Desktop.
Save mathdoodle/04267588af5347959827 to your computer and use it in GitHub Desktop.
Linear Perspective Calculation
{
"uuid": "055a26fa-11eb-4f67-97bb-f93dd8664668",
"description": "Linear Perspective Calculation",
"dependencies": {
"DomReady": "latest",
"davinci-blade": "1.1.1"
},
"operatorOverloading": true
}
<!doctype html>
<html>
<head>
<style>
/* STYLE-MARKER */
</style>
<!-- SCRIPTS-MARKER -->
</head>
<body>
<pre id='output'></pre>
<script>
// CODE-MARKER
</script>
</body>
</html>
DomReady.ready(main);
function main() {
try {
calculation();
}
catch(e) {
function red(arg: any) {
return "<span style='color:red'>" + arg + "</span>";
}
println(red(e.message));
}
}
var e1 = blade.vectorE3(1,0,0);
var e2 = blade.vectorE3(0,1,0);
var e3 = blade.vectorE3(0,0,1);
var I = e1 * e2 * e3;
/**
* Linear Perspective Calculation.
*/
function calculation() {
// Input variables
var P = blade.vectorE3(1.0, 0.5, -1.5);
var eye = blade.vectorE3(4,4,4);
var look = blade.vectorE3(0,1,0);
var up = blade.vectorE3(0,1,0);
var near = 1;
showvar('P', P);
showvar('eye', eye);
showvar('look', look);
showvar('up', up);
showvar('near', near);
println('----------')
var n = eye - look;
n = n / n.norm();
showvar('n', n.toFixed(4));
var B1 = n ^ up;
B1 = B1 / B1.norm();
showvar('B<sub>1</sub> = n ^ up', B1.toFixed(4));
var u = I * B1; // up.cross(n)
showvar('u = up &times; n = I * B<sub>1</sub>', u.toFixed(4));
var v = n.cross(u);
showvar('v = n &times; u = I * B<sub>1</sub>', v.toFixed(4));
showvar('u &times; v', u.cross(v).toFixed(4));
var Q = eye - (near/(n << (P-eye))) * (P-eye);
showvar('Q', Q.toFixed(4));
}
/**
* Print the HTML string without a line ending.
*/
function print(html: string): void {
var element = document.getElementById('output');
element.innerHTML = element.innerHTML + html;
}
/**
* Print the HTML string and go to the next line.
*/
function println(html: string): void {
print(html + '\n');
}
function showvar(name: string, value: any) {
println("<b>" + name + "</b> => " + value)
}
#output {
position: absolute;
left: 400px;
top: 100px;
font-size: 26pt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment