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
57 changes: 47 additions & 10 deletions modules/ecs6-class/line.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Point = require("./point");
const Point = require('./point');

class Line {
constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) {
Expand All @@ -9,13 +9,22 @@ class Line {
}

calculateSlope() {
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
}
if (this.point1.x == this.point2.x) {
throw new Error("it is not possible to divide by zero")
}
this.slope = (this.point2.y - this.point1.y) / (this.point2.x - this.point1.x)

}
calculateNOfLineFunction() {
if(!this.slope){
this.calculateSlope()
}
if (this.point1 == undefined) {
throw new Error('must to be two points')
}
this.n = this.point1.y - this.slope * this.point1.x
}

}
getPointOnXAsis() {
return this.getPointByY(0)
}
Expand All @@ -24,16 +33,44 @@ class Line {
return this.getPointByX(0)
}


getPointByX(x) {
let y = this.slope * x + this.n
return new Point({ x, y })

if (!this.slope) {
this.calculateSlope()
}
if (!this.n) {
this.calculateNOfLineFunction()
}
if (typeof (x) !== "number") {
throw new Error("x must be a number");
}
if (this.slope === 0) {
throw new Error("cant divide by zero");
}
else {
let y = this.slope * x + this.n;
return new Point({ x, y })
}
}

getPointByY(y) {
let x = (y - this.n) / this.slope;
return new Point({ x, y })
if (!this.slope) {
this.calculateSlope()
}
if (!this.n) {
this.calculateNOfLineFunction()
}
if (typeof (y) !== "number") {
throw new Error("y must be a number");
}
if (this.slope === 0) {
throw new Error("cant divide by zero");
}
else {
let x = (y - this.n) / this.slope;
return new Point({ x, y })
}
}
}

module.exports = Line
module.exports = Line
11 changes: 9 additions & 2 deletions modules/ecs6-class/point.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
class Point {
constructor({x=0, y=0}={}) {
constructor({ x = 0, y = 0 } = {}) {
this.x = x;
this.y = y;
}

moveVertical(value) {
if(typeof (value) != "number"){
throw new Error("the type must be a number");
}
this.y += value;
}

moveHorizontal(value) {
if(typeof (value) != "number"){
throw new Error("the type must be a number");
}
this.x += value;
}
}

module.exports = Point
49 changes: 37 additions & 12 deletions modules/geometry-calculation.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,65 @@
const Point = require('.ecs6-class/point');
const Line = require('./ecs6-class/line')

const calculateDistance = (point1, point2) => {
if(!(point1 instanceof Point) || (point2 instanceof Point)) {
throw new Error("point1 or 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.slope === line2.slope) {
if (line1.n === line2.n) {
return true
if (!(line1 instanceof Line) || !(line2 instanceof Line)) {
throw new Error("line1 or line2 is not Line");
}
line2.calculateSlope()
isPointOnLine(line1, line1.point2)
line2.calculateNOfLineFunction()
if (line1 == undefined || line2 == undefined) {
throw new Error("must to be 2 lines");
}
if (line1.slope === line2.slope) {
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
}

}

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


module.exports = {
calculateDistance,
calculateJunctionPoint,
Expand Down
Loading