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
27 changes: 27 additions & 0 deletions modules/ecs6-class/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ const Point = require("./point");

class Line {
constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) {
if (!(point1 instanceof Point) || !(point2 instanceof Point)) {
throw new Error('argument must be type point')
}
if (typeof (n) != "number" && n !== undefined) {
throw new Error('argument n must be type number')
}
if (typeof (slope) !== "number" && slope != undefined) {
throw new Error('argument slope must be type number')
}
this.point1 = point1;
this.point2 = point2;
this.slope = slope;
Expand All @@ -13,6 +22,9 @@ class Line {
}

calculateNOfLineFunction = () => {
if (this.slope == undefined) {
this.calculateSlope()
}
this.n = this.point1.y - this.slope * this.point1.x
}

Expand All @@ -26,11 +38,26 @@ class Line {


getPointByX(x) {
if (typeof (x) != "number") {
throw new Error('argument must be type number')
}
if (!this.n) {
this.calculateNOfLineFunction()
}
if (!this.slope) {
this.calculateSlope()
}
let y = this.slope * x + this.n
return new Point({ x, y })
}

getPointByY(y) {
if (typeof (y) != "number") {
throw new Error('argument must be type number')
}
if (!this.n || !this.slope) {
throw new Error("missing argument")
}
let x = (y - this.n) / this.slope;
return new Point({ x, y })
}
Expand Down
14 changes: 13 additions & 1 deletion modules/ecs6-class/point.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
class Point {
constructor({x=0, y=0}={}) {
constructor({ x = 0, y = 0 } = {}) {
if (typeof (x) !== "number") {
throw new Error('argument x must be type number')
}
if (typeof (y) !== "number") {
throw new Error('argument y must be type number')
}
this.x = x;
this.y = y;
}
moveVertical(value) {
if (typeof (value) !== "number") {
throw new Error('argument must be type number')
}
this.y += value;
}
moveHorizontal(value) {
if (typeof (value) !== "number") {
throw new Error('argument must be type number')
}
this.x += value;
}
}
Expand Down
15 changes: 14 additions & 1 deletion modules/geometry-calculation.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
const Line = require('./ecs6-class/line')
const Point = require('./ecs6-class/point')

const calculateDistance = (point1, point2) => {
if (!point1 || !point2) {
throw new Error('missing data')
}
if (!(point1 instanceof Point) || !(point2 instanceof Point)) {
throw new Error('argument must by type 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 one is not a 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 == undefined || line2 == undefined) {
throw new Error("missing data")
Copy link
Owner

Choose a reason for hiding this comment

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

which one is undefined
And what happens if line1 is a string, or a number? or line2 is also a string or a number?

}
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.

Who said that the slope is already calculated? (from both arguments)

if (line1.n === line2.n) {
Copy link
Owner

Choose a reason for hiding this comment

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

Who said the the n is already calculated?

return true
Expand All @@ -24,6 +34,9 @@ const calculateJunctionPoint = (line1, line2) => {
}

const isPointOnLine = (line, point) => {
if (!line || !line.slope || !line.n || !point) {
throw new Error("missing data")
Copy link
Owner

Choose a reason for hiding this comment

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

write tests that line = {slope:6, n:1} and point:56
what happens?

Copy link
Owner

Choose a reason for hiding this comment

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

check the basics and then you can calculate the missing data

}
const proxyLine = new Line({ point1: line.point1, point2: point })
proxyLine.calculateSlope()
if (line.slope === proxyLine.slope) {
Expand Down
Loading