Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
var lineList = [];
var pointList = [];
var intersectList = [];
var pointClick, bst, pq;
var sweepLine;
var dragLine;
var treeText;
//--------------------------------------------
var clickPoint2;
var pointVisual;
//--------------------------------------------
paper.install(window);
window.onload = function() {
var canvas = document.getElementById('myCanvas');
paper.setup(canvas);
fillCanvasBackground('white')
view.onResize = function(event){
fillCanvasBackground('white')
clearGraph();
}
view.onMouseDown = function(event) {
//----------------------------
if(pointClick){
pointVisual.fillColor = null;
pointVisual = null;
var clashPoint = checkPoints(event.point);
if(clashPoint)
clickPoint2 = clashPoint;
else
clickPoint2 = event.point;
addLine(pointClick, clickPoint2, 'line' + lineList.length);
pointList.push(pointClick);
pointList.push(clickPoint2);
pointClick = null;
clickPoint2 = null;
}
else{
//----------------------------
dragLine = new Path(); //Create line to show mouse drag
dragLine.strokeColor = "blue";
dragLine.strokeWidth = 3;
dragLine.strokeCap = 'round'
dragLine.dashArray = [5,6]
var clashPoint = checkPoints(event.point);
if(clashPoint)
pointClick = clashPoint;
else
pointClick = event.point;
pointVisual = new Path.Circle(pointClick, 5);
pointVisual.style = {
fillColor: new Color(1, 0, 0),
};
}
}
view.onMouseDrag = function(event) {
dragLine.add(event.point); //add to drag segment
}
view.onMouseUp = function(event) {
if(!dragLine.isEmpty() && pointVisual != null){
pointVisual.fillColor = null;
pointVisual = null;
}
dragLine.remove(); //Remove the drag seg and replace with line
if (pointClick) {
var endpoint = event.point;
if(checkPoints(endpoint))
endpoint = checkPoints(endpoint);
if (pointClick.getDistance(endpoint) > 20) {
addLine(pointClick, endpoint, 'line' + lineList.length);
pointList.push(pointClick);
pointList.push(endpoint);
pointClick = null;
}
}
}
sweepLine = new Path();
sweepLine.strokeColor = 'green';
sweepLine.add(new Point(-5, -2000), new Point(-5, 2000));
treeText = new PointText(new Point(50, 50));
treeText.justification = 'left';
treeText.fillColor = 'black';
treeText.name = 'treetext';
// Draw the view now:
view.draw();
}
/*
* Fill Background of Canvas
* @param color::String -> color to fill
*/
function fillCanvasBackground(color) {
var rec = new Path.Rectangle({
point: [0,0],
size: [view.size.width, view.size.height],
strokeColor: color,
})
rec.sendToBack()
rec.fillColor = color;
}
function addLine(p1, p2, name = '') {
var line = new Path();
var dx = p1.x - p2.x;
var dy = p1.y - p2.y;
line.strokeColor = 'black';
line.add(p1, p2);
line.name = name;
p1.line = line;
p2.line = line;
if (dx < 0 || (dx == 0 && dy < 0)) {
p1.left = 1;
line.data.originalY = p1.y;
}
else if (dx > 0 || (dx == 0 && dy >= 0)) {
p2.left = 1;
line.data.originalY = p2.y;
}
// Clear priority queue to reinitialize every time graph changes
if (pq)
pq.clear();
var text = new PointText(line.segments[0].point);
text.justification = 'right';
text.fillColor = 'black';
text.content = line.name;
lineList.push(line);
}
function initLineSweep() {
clearGraph(true);
bst = new BST();
pq = new PriorityQueue(pointList.length);
lineList.forEach(function(l) {
l.data.y = l.data.originalY;
l.strokeColor = 'black';
});
pointList.forEach(function(point) {
pq.insert([point.x, point.y], point);
});
}
function runLineSweep() {
if (!pq || pq.empty()) {
initLineSweep();
}
lineSweep(bst, pq, sweepLine).forEach(function(p) {
var c = new Path.Circle(new Point(p.x, p.y), 5);
c.fillColor = 'red';
intersectList.push(c);
});
treeText.content = bst.print(bst.root);
}
function clearGraph(intersectOnly) {
sweepLine.position = new Point(-10, sweepLine.position.y);
if (!intersectOnly) {
for (var i = 0; i < lineList.length; i++) {
lineList[i].remove();
}
project.getItems({ class: PointText }).forEach(function(pt) {
if (pt.name != 'treetext')
pt.remove();
});
pointList = [];
lineList = [];
}
for (var i = 0; i < intersectList.length; i++) {
intersectList[i].remove();
}
intersectList = [];
treeText.content = '';
}
function checkPoints(point) {
for(var i=0; i<pointList.length; i++){
if(point.getDistance(pointList[i]) < 20)
return pointList[i];
}
return false;
}
function runConvTest() {
//sort so lines are in order of connecting lines
var newLineList = sortLineList(lineList);
if(newLineList == false){
document.getElementById("failure").className = "";
return "failure";
}
//create a new ordered point list that doesn't include duplicate points
var newPointList = orderPoints(newLineList);
//call convexity test on the new list of points
var res = convexityTest(newPointList);
document.getElementById(res).className = "";
console.log(res);
return res;
}
//sort lines so each line in the list connects to the next
function sortLineList(lines){
var newLineList = [];
var length = lines.length;
newLineList.push(lines[0]);
lines.splice(0, 1);
var seg = newLineList[0].lastSegment;
for(var i=0; i<length; i++){
for(var j=0; j<lines.length; j++){
if((seg.point.x == lines[j].firstSegment.point.x && seg.point.y == lines[j].firstSegment.point.y)){
newLineList.push(lines[j]);
seg = lines[j].lastSegment;
lines.splice(j, 1);
}
else if((seg.point.x == lines[j].lastSegment.point.x && seg.point.y == lines[j].lastSegment.point.y)){
newLineList.push(lines[j]);
seg = lines[j].firstSegment;
lines.splice(j, 1);
}
}
}
if(newLineList.length != length){
console.log("points must connect");
return false;
}
return newLineList;
}
//order points so that leftmost is at beginning and there are no duplicates
function orderPoints(lines){
var pl1 = []
lines.forEach(function(path) {
var point = {
x: path.firstSegment.point.x,
y: path.firstSegment.point.y,
}
pl1.push(point);
});
var leftpoint = 0;
for(var i=1; i<pl1.length; i++){
if(pl1[i].x < pl1[leftpoint].x)
leftpoint = i;
}
var pl2 = pl1.splice(0, leftpoint);
var newPointList = pl1.concat(pl2);
return newPointList;
}