Created
October 22, 2010 17:17
-
-
Save lobodin/640976 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Point(x, y) { | |
this.x = x; | |
this.y = y; | |
} | |
function Figure(position) { | |
this.position = position; | |
} | |
function Line(position, endPosition) { | |
Figure.call(this, position); | |
this.endPosition = endPosition; | |
this.name = "Line"; | |
this.square = function() { | |
return 0; | |
} | |
} | |
Line.prototype = new Figure; | |
function Rectangle(position, endPosition) { | |
Line.call(this, position, endPosition); | |
this.name = "Rectangle" | |
this.square = function() { | |
return (this.endPosition.x - this.position.x) * | |
(this.endPosition.y - this.position.y); | |
} | |
} | |
Rectangle.prototype = new Line; | |
function Circle(position, radius) { | |
Figure.call(this, position); | |
this.radius = radius; | |
this.name = "Circle"; | |
this.square = function() { | |
return Math.PI * this.radius * this.radius; | |
} | |
} | |
Circle.prototype = new Figure; | |
var Intersection = (function() { | |
function LineAndLine(a, b) { | |
var x1 = a.position.x, y1 = a.position.y, | |
x2 = a.endPosition.x, y2 = a.endPosition.y, | |
x3 = b.position.x, y3 = b.position.y, | |
x4 = b.endPosition.x, y4 = b.endPosition.y; | |
var result = new Point; | |
result.x = ((x1*y2 - y1*x2)*(x3 - x4) - (x1 - x2)*(x3*y4 - y3*x4)) / | |
((x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4)); | |
result.y = ((x1*y2 - y1*x2)*(y3 - y4) - (y1 - y2)*(x3*y4 - y3*x4)) / | |
((x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4)); | |
return isNaN(result.x) || | |
Math.abs(result.x) != Infinity && | |
Math.abs(result.y) != Infinity && | |
result.x >= Math.min(x1, x2) && | |
result.x >= Math.min(x3, x4) && | |
result.y >= Math.min(y1, y2) && | |
result.y >= Math.min(y3, y4) && | |
result.x <= Math.max(x1, x2) && | |
result.x <= Math.max(x3, x4) && | |
result.y <= Math.max(y1, y2) && | |
result.y <= Math.max(y3, y4); | |
} | |
function convertRectangleToLines(rect) { | |
var topLeft = new Point(rect.position.x, rect.endPosition.y), | |
bottomRight = new Point(rect.endPosition.x, rect.position.y); | |
return [new Line(rect.position, topLeft), | |
new Line(topLeft, rect.endPosition), | |
new Line(rect.endPosition, bottomRight), | |
new Line(bottomRight, rect.position)]; | |
} | |
function LineAndRectangle(a, b) { | |
var lines = convertRectangleToLines(b); | |
for (var i in lines) if (LineAndLine(a, lines[i])) return true; | |
return false; | |
} | |
function RectangleAndRectangle(a, b) { | |
var linesA = convertRectangleToLines(a), | |
linesB = convertRectangleToLines(b); | |
for (var i in linesA) for (var j in linesB) if (LineAndLine(linesA[i], linesB[j])) return true; | |
return false; | |
} | |
function LineAndCircle(a, b) { | |
var x1 = a.position.x + b.position.x, | |
y1 = a.position.y + b.position.y; | |
x2 = a.endPosition.x + b.position.x; | |
y2 = a.endPosition.y + b.position.y; | |
var dr = Math.sqrt(Math.abs((x2 - x1)*(x2 - x1)) + Math.abs((y2 - y1)*(y2 - y1))), | |
D = x1*y2 - x2*y1; | |
return b.radius*b.radius*dr*dr - D*D >= 0; | |
} | |
function CircleAndRectangle(a, b) { | |
var lines = convertRectangleToLines(b); | |
for (var i in lines) if (LineAndCircle(lines[i], a)) return true; | |
return false; | |
} | |
function CircleAndCircle(a, b) { | |
var d = Math.sqrt(Math.pow(a.position.x - b.position.x, 2) + | |
Math.pow(a.position.y - b.position.y, 2)); | |
return d <= a.radius + b.radius && d >= Math.abs(a.radius - b.radius); | |
} | |
return { | |
doIntersect: function(a, b) { | |
if (!(a instanceof Figure && b instanceof Figure)) { | |
return false; | |
} | |
return a.name.length < b.name.length ? | |
eval(composeFunction(a, b, "(a, b)")) : | |
eval(composeFunction(b, a, "(b, a)")); | |
} | |
} | |
function composeFunction(a, b, args) { | |
return a.name + "And" + b.name + args; | |
} | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Figures</title> | |
<script type="text/javascript" src="figures.js"></script> | |
<script type="text/javascript"> | |
var line = new Line(new Point(0, 0), new Point(2, 2)); | |
var line1 = new Line(new Point(2, 0), new Point(1, 10)); | |
var rectangle = new Rectangle(new Point(1, 1), new Point(4, 3)); | |
var rectangle1 = new Rectangle(new Point(10, 10), new Point(20, 20)); | |
var circle = new Circle(new Point(2, 2), 3); | |
var circle1 = new Circle(new Point(0, 0), 1); | |
document.write(Intersection.doIntersect(line, line1)); | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment