JavaScript Shapes Task

A friend of mine had to complete task in JavaScript for his course at Telerik Academy but it was taking him too much time. He asked me for a help and I couldn’t refuse him.

Here are the requirements:

– Write functions for working with shapes in standard Planar coordinate system

– Points are represented by coordinates P(X, Y)

– Lines are represented by two points, marking their beginning and ending

– L(P1(X1,Y1),P2(X2,Y2))

– Calculate the distance between two points

– Check if three segment lines can form a triangle

So there was no time to loose. I started writing the code. I called the project “Galileo”.

Here’s the code:


galileo = new Object();
galileo.points = new Array();
galileo.lines = new Array();
galileo.add_point = function(x, y)
{
this.points.push({'x': x, 'y': y});
}
galileo.add_line = function(start_point, end_point)
{
var line = {
start: {
x: start_point.x,
y: start_point.y
},
end: {
x: end_point.x,
y: end_point.y
}
};
this.lines.push(line);
}
galileo.get_point = function(num)
{
return this.points[num];
}
galileo.get_points = function()
{
return this.points;
}
galileo.get_line = function(num)
{
return this.lines[num];
}
galileo.get_lines = function()
{
return this.lines;
}
galileo.calculate_distance = function(point1, point2)
{
var distance;
if (point1.x == point1.x && point2.y == point2.y)
{
distance = point1.y – point2.y;
}
// Same X coordinates, get line length
else if (point1.x == point2.x)
{
distance = point1.y – point2.y;
}
// Same Y coordinates, get line length
else if (point1.y == point2.y)
{
distance = point1.x – point2.x;
}
// If the figure is square the logic can be simplified
// but we'll just use the formula for rectangle
else
{
var side1_lengh = Math.abs(point1.x – point1.y);
var side2_lengh = Math.abs(point2.x – point2.y);
distance = Math.pow(side1_lengh, 2) – Math.pow(side2_lengh, 2);
}
return Math.abs(distance);
}
galileo.get_line_length = function(line)
{
return this.calculate_distance(
{
x: line.start.x,
y: line.start.y
},
{
x: line.end.x,
y: line.end.y
}
);
}
galileo.is_triangle = function(line1, line2, line3)
{
// Get each line length
var side1 = this.get_line_length(line1);
var side2 = this.get_line_length(line2);
var side3 = this.get_line_length(line3);
// We have to check if every side is equal or lower than the sum of the others
var side1_check = side1 <= (side2 + side3);
var side2_check = side2 <= (side1 + side3);
var side3_check = side3 <= (side2 + side3);
return side1_check && side2_check && side3_check;
}
// Step 1: Add points
galileo.add_point(1, 1); //point 0
galileo.add_point(2, 2); //point 1
galileo.add_point(1, 4); //point 2
// Debug: List all added points
var all_points = galileo.get_points();
console.log('All points:');
console.log(all_points);
// Get point whenever you need it,
// in our case we need all of the three points right now
var point1 = galileo.get_point(0);
var point2 = galileo.get_point(1);
var point3 = galileo.get_point(2);
// Step 2: Calculate distance between two points
//var distance = galileo.calculate_distance(point1, point3);
var distance = galileo.calculate_distance(point1, point2);
// Debug
console.log('Test points: (' + point1.x + ',' + point1.y + '), (' + point2.x + ', ' + point2.y + ')');
console.log('Distance: ' + distance);
// Step 3: Add lines
galileo.add_line(point1, point3);
galileo.add_line(point1, point2);
galileo.add_line(point2, point3);
// Debug: List all added lines
var all_lines = galileo.get_lines();
console.log('All lines:');
console.log(all_lines);
var line1 = galileo.get_line(0);
var line2 = galileo.get_line(1);
var line3 = galileo.get_line(2);
var is_triangle = galileo.is_triangle(line1, line2, line3);
if (is_triangle)
console.log('This is triangle!');
else
console.log('This is NOT a triangle!');

view raw

gistfile1.js

hosted with ❤ by GitHub

It took me about an hour but this is because I don’t use the coordinate system in my everyday work. I also added comments and debugging for each step so my friend can easily understand what I’ve written.

After a few days he said to me that the code was too complicated and he wrote the task in 20 lines. As my efforts seemed to be useless I decided to share this code with you. I hope it will be useful for someone else.