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

class Line {
constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) {

if (!(point1 instanceof Point)) {
throw new Error('point1 should be an instance of Point');
}

if (!(point2 instanceof Point)) {
throw new Error('point2 should be an instance of Point');
}

if (n !== undefined && typeof n !== 'number') {
throw new Error('n should be a number or undefined');
}

if (slope !== undefined && typeof slope !== 'number') {
throw new Error('slope should be a number or undefined');
}

this.point1 = point1;
this.point2 = point2;
this.slope = slope;
this.n = n;
}

calculateSlope = () => {
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)

if (this.point1.x === this.point2.x && this.point1.y === this.point2.y) {
throw Error('Both points have the same values ​​so it is not a line');
}

if (this.point1.x === this.point2.x) {
throw Error('x of Point1 is the same as x of point2. It is not a line');

}

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
Copy link
Owner

Choose a reason for hiding this comment

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

what happens if slope is undefined?

}


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


getPointByX(x) {
if (x === undefined)
throw new Error('x is undefined')

if (typeof x !== 'number')
throw new Error('x is not a number')

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

getPointByY(y) {
let x = (y - this.slope) / this.n;
if (y === undefined)
throw Error('y is undefined')

if (typeof y !== 'number')
throw Error('y is not a number')

let x = (y - this.n) / this.slope;
return new Point({ x, y })
}
}

module.exports = Line
module.exports = Line



27 changes: 25 additions & 2 deletions modules/ecs6-class/point.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@


class Point {
constructor({x=0, y=0}={}) {

constructor({ x = 0, y = 0 } = {}) {

if (typeof (x) !== 'number' && typeof (y) !== 'number')
throw new Error('x and y are not number')
if (typeof (x) !== 'number')
throw new Error('x is not number')
if (typeof (y) !== 'number')
throw new Error('y is not number')
this.x = x;
this.y = y;
}

moveVertical(value) {

if (!value)
throw new Error('value is undefined')
if (typeof value !== 'number')
throw new Error('value is not number')
this.y += value;
}


moveHorizontal(value) {
if (!value)
throw Error('value is undefined')
if (typeof value !== 'number')
throw Error('value is not number')

this.x += value;
}
}

module.exports = Point
module.exports =Point
42 changes: 0 additions & 42 deletions modules/geometry-calculation.js

This file was deleted.

96 changes: 96 additions & 0 deletions modules/geometry-calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const Line = require('./ecs6-class/line')
const Point = require('./ecs6-class/point')

const calculateDistance = (point1, point2) => {

if (!(point1 instanceof Point)) {
throw new Error('point1 is not a Point');
}
if (!(point2 instanceof Point)) {
throw new Error('point2 is not a Point');
}

let distanceX = (point2.x - point1.x) ** 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('line1 is not a Line')
}

if (!(line2 instanceof Line)) {
throw new Error('line2 is not a Line')
}

if(!line1.slope){
line1.calculateSlope()
}

if(!line2.slope){
line2.calculateSlope()
}

if(!line1.n){
line1.calculateNOfLineFunction()
}

if(!line2.n){
line2.calculateNOfLineFunction()
}

if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
return true
}
else {
return false
}
}
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)) {
throw new Error('line is not a Line')
}
if (!(point instanceof Point)) {
throw new Error('point is not a Point')
}
if(!line.slope){
line.calculateSlope()
}

if(!line.n){
line.calculateNOfLineFunction()
}


const proxyLine = new Line({ point1: line.point1, point2: point })
proxyLine.calculateSlope()
if (line.slope === proxyLine.slope) {
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.

what happens when the slope or n or both of them were not calculate yet?

return true
}
else {
return false;
}
}
return false
}

module.exports = {
calculateDistance,
calculateJunctionPoint,
isPointOnLine
}
18 changes: 11 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
"description": "practice unit tests in javascript",
"main": "index.js",
"scripts": {
"test": "jest"
},
"dependencies": {
"jest": "^29.7.0"
"test": "jest",
"test:coverage": "npm run test -- --coverage"
},

Copy link
Owner

Choose a reason for hiding this comment

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

Where is jest now?

"repository": {
"type": "git",
"url": "git+https://github.com/gemtechd/build-tests.git"
Expand All @@ -19,5 +18,4 @@
"homepage": "https://github.com/gemtechd/build-tests#readme",
"author": "gemtechd",
"license": "ISC"

}
Loading