Skip to content

Instantly share code, notes, and snippets.

@garciadelcastillo
Created December 12, 2014 16:32
Show Gist options
  • Save garciadelcastillo/100ef9c76bf56913e0ad to your computer and use it in GitHub Desktop.
Save garciadelcastillo/100ef9c76bf56913e0ad to your computer and use it in GitHub Desktop.
Sketchpad.js - Line intersections
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div id="sketchpadDiv">
<canvas id="sketchpadCanvas"></canvas>
</div>
</body>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="http://www.garciadelcastillo.es/sketchpad/sketchpad.js"></script>
<script type="text/javascript" src="sketch.js"></script>
</html>
// Create new instance of Sketchpad in target Canvas
var pad = new Sketchpad('sketchpadCanvas');
// Create some Nodes and join them with Lines
var A = new pad.Node(100, 100),
B = new pad.Node(540, 300),
C = new pad.Node(540, 100),
D = new pad.Node(100, 300),
AB = pad.Line.between(A, B),
CD = pad.Line.between(C, D);
// Compute the intersection of the Lines, and attach a Circle to it
var X = pad.Point.intersection(AB, CD),
circleX = pad.Circle.centerRadius(X, 101);
// Compute intersections between Circle and Lines
var XAB = pad.Point.intersection(AB, circleX),
XCD = pad.Point.intersection(CD, circleX);
// Display and Tag all Points
pad.showPoints();
pad.tagPoints();
body {
margin: 0;
padding: 0;
}
#sketchpadDiv {
position: absolute;
width: 100%;
height: 100%;
border: 1px solid #000;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#sketchpadCanvas {
position: absolute;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment