Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions modules/ecs6-class/line.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
const Point = require("./point");

class Line {
constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) {
this.point1 = point1;
this.point2 = point2;
this.slope = slope;
this.n = n;
constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) {
if((point1 instanceof Point) )
if((point2 instanceof Point)){
this.point1 = point1;
this.point2 = point2;
if(typeof slope === 'number' || typeof slope === 'undefined')
this.slope = slope;
else
throw new Error('the type of slope is not number');
if(typeof n === 'number' || typeof n === 'undefined')
this.n = n;
else
throw new Error('the type of n is not number');
}
else
throw new Error('the type of point2 is not Point')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throw directly an error, like this, you don't mix up checks and codes

else
throw new Error('the type of point1 is not Point')
}

calculateSlope = () => {
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
if((this.point1.y - this.point2.y) !== 0)
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
else
throw new Error('division by zero')
}

calculateNOfLineFunction = () => {
Expand All @@ -31,8 +47,12 @@ class Line {
}

getPointByY(y) {
let x = (y - this.n) / this.slope;
return new Point({ x, y })
if(this.slope !== 0){
let x = (y - this.n) / this.slope;
return new Point({ x, y })
}
else
throw new Error('division by zero')
}
}

Expand Down
11 changes: 10 additions & 1 deletion modules/ecs6-class/point.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
class Point {
constructor({x=0, y=0}={}) {
if(typeof x !== "number")
throw new Error('type of x is not number')
if(typeof(y) !== "number")
throw new Error('type of y is not number')
this.x = x;
this.y = y;
}
moveVertical(value) {
this.y += value;
if(typeof(value) !== "number")
throw new Error('the type of the value is not correctly')
this.y += value;
}
moveHorizontal(value) {
if(typeof(value) !== "number")
throw new Error('the type of the value is not correctly')
this.x += value;

}
}

Expand Down
34 changes: 28 additions & 6 deletions modules/geometry-calculation.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
const Line = require('./ecs6-class/line')
const Point = require('./ecs6-class/point')

const calculateDistance = (point1, point2) => {
if(!(point1 instanceof Point))
throw new Error('the type of point1 is not Point')
if(!(point2 instanceof Point))
throw new Error('the type of point2 is not Point')
let distanceX = (point2.x - point1.x) ** 2;
let distanceY = (point2.y - point2.y) ** 2;
let distanceY = (point2.y - point1.y) ** 2;
const distance = Math.sqrt(distanceX + distanceY);
return distance;
}

const calculateJunctionPoint = (line1, line2) => {
if(!(line1 instanceof Line))
throw new Error('the type of line1 is not Line')
if(!(line2 instanceof Line))
throw new Error('the type of line2 is not Line')
if(line1.slope === undefined)
line1.calculateSlope()
if(line2.slope === undefined)
line2.calculateSlope()
if(line1.n === undefined)
line1.calculateNOfLineFunction()
if(line2.n === undefined)
line2.calculateNOfLineFunction()
if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
return true
Expand All @@ -24,15 +41,20 @@ const calculateJunctionPoint = (line1, line2) => {
}

const isPointOnLine = (line, point) => {
if(!(line instanceof Line))
throw new Error('the type of line is not Line')
if(!(point instanceof Point))
throw new Error('the type of point is not Point')
const proxyLine = new Line({ point1: line.point1, point2: point })
proxyLine.calculateSlope()
if (line.slope === proxyLine.slope) {
proxyLine.calculateNOfLineFunction()
if (line.n === proxyLine.n) {
return true
if (line.slope === proxyLine.slope) {
proxyLine.calculateNOfLineFunction()
if (line.n === proxyLine.n) {
return true
}
}
}
return false

}

module.exports = {
Expand Down
Loading