Last active
January 16, 2021 15:02
-
-
Save skawnkk/c7f100ce0ef3b1baf3a69e71c34a46a8 to your computer and use it in GitHub Desktop.
OOP_SHAPE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#CLASS, #OBJECT, #Set(), #super #extends | |
let { | |
log | |
} = console; | |
//*도형 생성자$프로토타입 | |
//직선길이 | |
class Straight { | |
constructor(a, b) { | |
this.a = a; | |
this.b = b; | |
} | |
distanceBtwPoints() { | |
let p = [this.a, this.b] | |
let distance = Math.sqrt(Math.pow((this.a.x - this.b.x), 2) + Math.pow((this.a.y - this.b.y), 2)); | |
return distance.toFixed(3); | |
} | |
paintGraph() { | |
//graph구현 | |
const arr = new Array(25).fill(''); | |
for (let i = 0; i < arr.length; i++) { | |
arr[i] = new Array(25).fill(''); | |
} | |
for (let i = 0; i < arr.length; i++) { | |
if (i % 2 == 0) { | |
(i >= 10) ? arr[i][0] = `${i}|`: arr[i][0] = ` ${i}|`; | |
} else { | |
arr[i][0] = ' |'; | |
} | |
arr[0][i] = '———'; | |
} | |
arr[0][0] = ' +'; | |
//좌표 찍기 | |
const points = [this.a, this.b, this.c, this.d]; | |
for (let point of points) { | |
if (point !== undefined) { | |
let blank = ''; | |
for (let i = 0; i < point.x; i++) { | |
blank += ' '; | |
} | |
arr[point.y][point.x] = `${blank}*`; | |
} | |
} | |
//graph출력 | |
for (let i = 24; i >= 0; i--) { | |
log(arr[i].join('')) | |
} | |
let lineX = ''; | |
for (let i = 0; i < arr.length; i++) { | |
if (i % 2 == 0) { | |
lineX += ` ${i}`; | |
} | |
} | |
log(lineX) | |
} | |
} | |
//삼각형과 너비 | |
class Triangle extends Straight { | |
constructor(a, b, c) { | |
super(a, b) | |
this.c = c; | |
} | |
triangleArea() { | |
let sideA = new Straight(this.a, this.b); | |
let sideB = new Straight(this.b, this.c); | |
let sideC = new Straight(this.c, this.a); | |
let a = +sideA.distanceBtwPoints(); | |
let b = +sideB.distanceBtwPoints(); | |
let c = +sideC.distanceBtwPoints(); | |
let s = (a + b + c) / 2; | |
let area = Math.sqrt(s * (s - a) * (s - b) * (s - c)); | |
return area.toFixed(3); | |
} | |
} | |
//사각형과 너비 | |
class Square extends Straight { | |
constructor(a, b, c, d) { | |
super(a, b); | |
this.c = c; | |
this.d = d; | |
} | |
errorCheck() { | |
//에러처리 | |
//점은 4개지만 사각형이 아닌 경우(직선, ㄱ모양) | |
//마름모 or 사다리꼴 __ 직사각형이 아닌 경우 | |
const duplicatedDeleteX = new Set([this.a.x, this.b.x, this.c.x, this.d.x]) | |
const duplicatedDeleteY = new Set([this.a.y, this.b.y, this.c.y, this.d.y]) | |
if (duplicatedDeleteX.size !== 2 && duplicatedDeleteY.size !== 2) { | |
log('▶ 좌표값이 잘못됬어요. 직사각형이 아닙니닷!! ╰(‵□′)╯'); | |
return false; | |
} | |
} | |
squareArea() { | |
let sideB = new Straight(this.b, this.c); | |
let sideA = new Straight(this.a, this.b); | |
let sideC = new Straight(this.c, this.d); | |
let sideD = new Straight(this.d, this.a); | |
let a = +sideA.distanceBtwPoints(); | |
let b = +sideB.distanceBtwPoints(); | |
let c = +sideC.distanceBtwPoints(); | |
let d = +sideD.distanceBtwPoints(); | |
let area = new Set([a, b, c, d]); | |
area = Array.from(area); | |
return +(area[0] * area[1]).toFixed(4); | |
} | |
} | |
//*측정도형선택하기 | |
function matchingShape(input) { | |
let point1 = { | |
x: input[0], | |
y: input[1] | |
}; | |
let point2 = { | |
x: input[2], | |
y: input[3] | |
}; | |
let point3 = { | |
x: input[4], | |
y: input[5] | |
}; | |
let point4 = { | |
x: input[6], | |
y: input[7] | |
}; | |
switch (input.length) { | |
case 4: | |
const length = new Straight(point1, point2); | |
length.paintGraph(); | |
log(`▶ 두 점 사이의 거리는 ${length.distanceBtwPoints()} 입니다.`); | |
break; | |
case 6: | |
const triangle = new Triangle(point1, point2, point3); | |
triangle.paintGraph(); | |
if (triangle.triangleArea() != 0) { | |
log(`▶ 삼각형의 넓이는 ${triangle.triangleArea()} 입니다.`) | |
} else { | |
log('▶ 직선이네요?') | |
} | |
break; | |
case 8: | |
const square = new Square(point1, point2, point3, point4); | |
square.paintGraph(); | |
if (square.errorCheck() !== false) { | |
log(`▶ 사각형의 넓이는 ${square.squareArea()} 입니다.`); | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
//*좌표 입력받기 | |
const init = () => { | |
const readline = require("readline"); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
log("▶ 좌표입력_EX: (x1, y1)-(x2,y2)"); | |
rl.on("line", function (line) { | |
let input = line.replace(/[^0-9]/gi, ",").split(",").filter(el => el !== ''); | |
for (let num of input) { | |
num = parseInt(num); | |
if (num > 24 || num < 0) { | |
log('!!__ 입력범위초과__0<(x,y)<24'); | |
rl.prompt(); | |
} | |
} | |
matchingShape(input); | |
if (line === "exit") rl.close(); | |
}).on("close", function () { | |
process.exit(); | |
}); | |
} | |
init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
prototype ->class