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
28 changes: 22 additions & 6 deletions modules/ecs6-class/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ class Line {
this.point2 = point2;
this.slope = slope;
this.n = n;
if ((this.slope !== undefined && typeof (this.slope) !== 'number') || (this.n !== undefined && typeof (this.n) !== 'number'))
throw Error('this value is not a number')
Copy link
Owner

Choose a reason for hiding this comment

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

which parameter is not a number?

}

calculateSlope = () => {
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
if (this.point1.x === this.point2.x)
throw Error("Division by zero occurred")
else
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
}

calculateNOfLineFunction = () => {
this.n = this.point1.y - this.slope * this.point1.x
}
Expand All @@ -26,13 +30,25 @@ class Line {


getPointByX(x) {
let y = this.slope * x + this.n
return new Point({ x, y })
if (typeof (x) !== "number")
throw Error("this value is not a number")
else {
let y = this.slope * x + this.n
return new Point({ x, y })
}
}

getPointByY(y) {
let x = (y - this.slope) / this.n;
return new Point({ x, y })
if (typeof (y) !== "number")
throw Error("this value is not a number")
else {
if (this.slope === 0)
throw Error("Division by zero occurred")
else {
let x = (y - this.n) / this.slope;
return new Point({ x, y })
}
}
}
}

Expand Down
17 changes: 13 additions & 4 deletions modules/ecs6-class/point.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
class Point {
constructor({x=0, y=0}={}) {
this.x = x;
this.y = y;
constructor({ x = 0, y = 0 } = {}) {
if (typeof (x) !== "number" || typeof (y) !== "number")
throw Error("this value is not a number")
Copy link
Owner

Choose a reason for hiding this comment

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

who is not a number, the x or the y?

else {
this.x = x;
this.y = y;
}
}

moveVertical(value) {
if (typeof (value) !== "number")
throw Error("this value is not a number")
this.y += value;
}
moveHorizontal(value) {
if (typeof (value) !== "number")
throw Error("this value is not a number")
this.x += value;
}
}

module.exports = Point
module.exports = Point
57 changes: 36 additions & 21 deletions modules/geometry-calculation.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
const Line = require('./ecs6-class/line')
const Line = require('./ecs6-class/line');
const Point = require('./ecs6-class/point');

const calculateDistance = (point1, point2) => {
let distanceX = (point2.x - point1.x) ** 2;
let distanceY = (point2.y - point2.y) ** 2;
const distance = Math.sqrt(distanceX + distanceY);
return distance;
if (point1 instanceof Point || point2 instanceof Point) {
let distanceX = (point2.x - point1.x) ** 2;
let distanceY = (point2.y - point1.y) ** 2;
const distance = Math.sqrt(distanceX + distanceY);
return distance;
}
else
throw Error("this point is not a Point")
Copy link
Owner

Choose a reason for hiding this comment

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

which parameter is not a point
and it will be good if I would be noticed if a parameter in undefined

}


const calculateJunctionPoint = (line1, line2) => {
if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
return true
if (line1 instanceof Line && line2 instanceof Line) {
if (line1.slope === line2.slope) {
Copy link
Owner

Choose a reason for hiding this comment

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

Is the slope already calculated?

Copy link
Author

Choose a reason for hiding this comment

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

I didn't realize the slope is already calculated

Copy link
Owner

Choose a reason for hiding this comment

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

It doesn't have to be
it can be undefined

if (line1.n === line2.n) {
return true
}
else {
return false
}
}
else {
return false
const x = (line1.n - line2.n) / (line2.slope - line1.slope)
const junctionPoint = line1.getPointByX(x);
return junctionPoint
}
}
else {
const x = (line1.n - line2.n) / (line2.slope - line1.slope)
const junctionPoint = line1.getPointByX(x);
return junctionPoint
}
else
throw Error("this line is not a Line")
Copy link
Owner

Choose a reason for hiding this comment

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

which line is not a line?

}


const isPointOnLine = (line, point) => {
const proxyLine = new Line(line.point1, point)
proxyLine.calculateSlope()
if (line.slope === proxyLine.slope) {
proxyLine.calculateNOfLineFunction()
if (line.n === proxyLine.n2) {
return true
if ((line instanceof Line) && (point instanceof Point)) {
const proxyLine = new Line({ point1: line.point1, point2: point })
proxyLine.calculateSlope()
if (line.slope === proxyLine.slope) {
Copy link
Owner

Choose a reason for hiding this comment

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

the slope of the line is already calculated?

proxyLine.calculateNOfLineFunction()
if (line.n === proxyLine.n) {
Copy link
Owner

Choose a reason for hiding this comment

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

is the n of the line already calculated

return true
}
}
return false
}
return false
else
throw Error("this object is not an Line or Point")
Copy link
Owner

Choose a reason for hiding this comment

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

a clearer error message

}

module.exports = {
Expand Down
Loading