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
37 changes: 37 additions & 0 deletions modules/ecs6-class/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ class Line {

calculateSlope() {
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
if (!this.slope) {
this.slope = 0
}
}

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

Expand All @@ -22,16 +28,47 @@ class Line {

getPointOnYAsis() {
return this.getPointByX(0)
TODO//should i make here a mock testing , because of sendindg to another function?
}


getPointByX(x) {
if (x === undefined) {
throw new Error('no argument was sent')
}
if (typeof (x) !== "number") {
throw new Error('argument is not a number')
}
if (!this.slope) {
this.calculateSlope()
}
if (!this.n) {
this.calculateNOfLineFunction()
}
let y = this.slope * x + this.n
return new Point({ x, y })
}

getPointByY(y) {
if (y === undefined) {
throw new Error('no argument was sent')
}
if (typeof (y) !== "number") {
throw new Error('argument is not a number')
}
if (!this.slope) {
this.calculateSlope()
}
if (!this.n) {
this.calculateNOfLineFunction()
}
let x = (y - this.n) / this.slope;
// console.log(x);
// if (!x) {
// return new Point({ x: 0, y })
// }
//TODO what will i do with y=0? what does have to be in x?

return new Point({ x, y })
}
}
Expand Down
8 changes: 7 additions & 1 deletion modules/ecs6-class/point.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
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("value is NaN")
}
this.y += value;
}
moveHorizontal(value) {
if (typeof (value) !== "number") {
throw new Error("value is NaN")
}
this.x += value;
}
}
Expand Down
35 changes: 33 additions & 2 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 Line = require('./ecs6-class/line');
const Point = require('./ecs6-class/point');

const calculateDistance = (point1, point2) => {
if(!point1 || !point2){
throw new Error('missing required arguments')
}

if(!(point1 instanceof Point) || !(point2 instanceof Point)){
throw new Error ("argument not match the required type")
}

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 || !line2){
throw new Error('missing required arguments')
}

if(!(line1 instanceof Line) || !(line2 instanceof Line)){
throw new Error ("argument not match the required type")
}

if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
return true
Expand All @@ -24,10 +41,24 @@ const calculateJunctionPoint = (line1, line2) => {
}

const isPointOnLine = (line, point) => {
if(!line || !point){
throw new Error('missing required arguments')
}

if(!(line instanceof Line) || !(point instanceof Point)){
throw new Error ("argument not match the required type")
}

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