할당완료

This commit is contained in:
yscha 2025-11-30 23:37:03 +09:00
parent 2ac1e80964
commit 8d6319cc46
2 changed files with 204 additions and 69 deletions

View File

@ -1059,7 +1059,7 @@ export const usePolygon = () => {
for (let i = divideLines.length - 1; i >= 0; i--) {
const line = divideLines[i]
const { intersections, startPoint, endPoint } = line
console.log("intersections::::::::::", intersections)
if (intersections.length === 1) {
const newLinePoint1 = [line.x1, line.y1, intersections[0].x, intersections[0].y]
const newLinePoint2 = [intersections[0].x, intersections[0].y, line.x2, line.y2]
@ -1387,7 +1387,7 @@ export const usePolygon = () => {
let representLine
// 지붕을 그리면서 기존 polygon의 line중 연결된 line을 찾는다.
;[...polygonLines, ...innerLines].forEach((line) => {
[...polygonLines, ...innerLines].forEach((line) => {
let startFlag = false
let endFlag = false
const startPoint = line.startPoint
@ -1490,6 +1490,7 @@ export const usePolygon = () => {
})
roofLines.forEach((line) => {
console.log("::::::::::",line);
roof.lines.forEach((roofLine) => {
if (
(isSamePoint(line.startPoint, roofLine.startPoint) && isSamePoint(line.endPoint, roofLine.endPoint)) ||
@ -1583,52 +1584,126 @@ export const usePolygon = () => {
// ==== Dijkstra pathfinding ====
// function findShortestPath(start, end, graph, epsilon = 1) {
// const startKey = pointToKey(start, epsilon)
// const endKey = pointToKey(end, epsilon)
//
// const distances = {}
// const previous = {}
// const visited = new Set()
// const queue = [{ key: startKey, dist: 0 }]
//
// for (const key in graph) distances[key] = Infinity
// distances[startKey] = 0
//
// while (queue.length > 0) {
// queue.sort((a, b) => a.dist - b.dist)
// const { key } = queue.shift()
// if (visited.has(key)) continue
// visited.add(key)
//
// for (const neighbor of graph[key] || []) {
// const neighborKey = pointToKey(neighbor.point, epsilon)
// const alt = distances[key] + neighbor.distance
// if (alt < distances[neighborKey]) {
// distances[neighborKey] = alt
// previous[neighborKey] = key
// queue.push({ key: neighborKey, dist: alt })
// }
// }
// }
//
// const path = []
// let currentKey = endKey
//
// if (!previous[currentKey]) return null
//
// while (currentKey !== startKey) {
// const [x, y] = currentKey.split(',').map(Number)
// path.unshift({ x, y })
// currentKey = previous[currentKey]
// }
//
// const [sx, sy] = startKey.split(',').map(Number)
// path.unshift({ x: sx, y: sy })
//
// return path
// }
function findShortestPath(start, end, graph, epsilon = 1) {
const startKey = pointToKey(start, epsilon)
const endKey = pointToKey(end, epsilon)
const startKey = pointToKey(start, epsilon);
const endKey = pointToKey(end, epsilon);
const distances = {}
const previous = {}
const visited = new Set()
const queue = [{ key: startKey, dist: 0 }]
// 거리와 이전 노드 추적
const distances = { [startKey]: 0 };
const previous = {};
const visited = new Set();
for (const key in graph) distances[key] = Infinity
distances[startKey] = 0
// 우선순위 큐 (거리가 짧은 순으로 정렬)
const queue = [{ key: startKey, dist: 0 }];
while (queue.length > 0) {
queue.sort((a, b) => a.dist - b.dist)
const { key } = queue.shift()
if (visited.has(key)) continue
visited.add(key)
// 모든 노드 초기화
for (const key in graph) {
if (key !== startKey) {
distances[key] = Infinity;
}
}
for (const neighbor of graph[key] || []) {
const neighborKey = pointToKey(neighbor.point, epsilon)
const alt = distances[key] + neighbor.distance
if (alt < distances[neighborKey]) {
distances[neighborKey] = alt
previous[neighborKey] = key
queue.push({ key: neighborKey, dist: alt })
// 우선순위 큐에서 다음 노드 선택
const getNextNode = () => {
if (queue.length === 0) return null;
queue.sort((a, b) => a.dist - b.dist);
return queue.shift();
};
let current;
while ((current = getNextNode())) {
const currentKey = current.key;
// 목적지에 도달하면 종료
if (currentKey === endKey) break;
// 이미 방문한 노드는 건너뜀
if (visited.has(currentKey)) continue;
visited.add(currentKey);
// 인접 노드 탐색
for (const neighbor of graph[currentKey] || []) {
const neighborKey = pointToKey(neighbor.point, epsilon);
if (visited.has(neighborKey)) continue;
const alt = distances[currentKey] + neighbor.distance;
// 더 짧은 경로를 찾은 경우 업데이트
if (alt < (distances[neighborKey] || Infinity)) {
distances[neighborKey] = alt;
previous[neighborKey] = currentKey;
// 우선순위 큐에 추가
queue.push({ key: neighborKey, dist: alt });
}
}
}
const path = []
let currentKey = endKey
// 경로 재구성
const path = [];
let currentKey = endKey;
if (!previous[currentKey]) return null
while (currentKey !== startKey) {
const [x, y] = currentKey.split(',').map(Number)
path.unshift({ x, y })
currentKey = previous[currentKey]
// 시작점에 도달할 때까지 역추적
while (previous[currentKey] !== undefined) {
const [x, y] = currentKey.split(',').map(Number);
path.unshift({ x, y });
currentKey = previous[currentKey];
}
const [sx, sy] = startKey.split(',').map(Number)
path.unshift({ x: sx, y: sy })
// 시작점 추가
if (path.length > 0) {
const [sx, sy] = startKey.split(',').map(Number);
path.unshift({ x: sx, y: sy });
}
return path
return path.length > 0 ? path : null;
}
// 최종 함수
function getPath(start, end, graph, epsilon = 1) {
// startPoint와 arrivalPoint가 될 수 있는 점은 line.attributes.type이 'default' 혹은 null이 아닌 line인 경우에만 가능

View File

@ -857,18 +857,21 @@ if(roof.moveUpDown??0 > 0) {
newPStart.y = wallBaseLine.y1;
getAddLine({ x: newPEnd.x, y: wallBaseLine.y1 }, { x: wallBaseLine.x1, y: wallBaseLine.y1 })
findPoints.push({ x: wallBaseLine.x1, y: wallBaseLine.y1 });
} else if (wallBaseLine.y2 <= newPEnd.y && newPEnd.y <= wallBaseLine.y1 && wallBaseLine.y1 <= newPStart.y) {
} else if (wallBaseLine.y2 <= newPEnd.y && newPEnd.y <= wallBaseLine.y1 && wallBaseLine.y1 <= newPStart.y) { //bottom right
newPEnd.y = wallBaseLine.y2;
getAddLine({ x: newPEnd.x, y: wallBaseLine.y2 }, { x: wallBaseLine.x2, y: wallBaseLine.y2 })
findPoints.push({ x: wallBaseLine.x2, y: wallBaseLine.y2 });
} else if (newPStart.y <= wallBaseLine.y1 && wallBaseLine.y1 <= newPEnd.y && newPEnd.y <= wallBaseLine.y2) { //top left
newPEnd.y = wallBaseLine.y2;
getAddLine({ x: newPEnd.x, y: wallBaseLine.y2 }, { x: wallBaseLine.x2, y: wallBaseLine.y2 })
findPoints.push({ x: wallBaseLine.x2, y: wallBaseLine.y2 });
} else if (wallBaseLine.y1 <= newPStart.y && newPStart.y <= wallBaseLine.y2 && wallBaseLine.y2 <= newPEnd.y) {
} else if (wallBaseLine.y1 <= newPStart.y && newPStart.y <= wallBaseLine.y2 && wallBaseLine.y2 <= newPEnd.y) { //bottom left
newPStart.y = wallBaseLine.y1;
getAddLine({ x: newPEnd.x, y: wallBaseLine.y1 }, { x: wallBaseLine.x1, y: wallBaseLine.y1 })
findPoints.push({ x: wallBaseLine.x1, y: wallBaseLine.y1 });
} else if (wallBaseLine.y2 <= newPEnd.y && newPEnd.y <= newPStart.y && newPStart.y <= wallBaseLine.y1) { // 위가운데
newPEnd.y = wallBaseLine.y2;
@ -901,14 +904,20 @@ if(roof.moveUpDown??0 > 0) {
getAddLine({ x: wallBaseLine.x2, y: newPEnd.y }, { x: wallBaseLine.x2, y: wallBaseLine.y2 })
//추가 라인?
findPoints.push({ x: wallBaseLine.x2, y: wallBaseLine.y2 });
} else if (newPStart.x <= wallBaseLine.x1 && wallBaseLine.x1 <= newPEnd.x && newPEnd.x <= wallBaseLine.x2) { //위 오른쪽
} else if (newPStart.x <= wallBaseLine.x1 && wallBaseLine.x1 <= newPEnd.x && newPEnd.x <= wallBaseLine.x2) { //bottom left
newPEnd.x = wallBaseLine.x2;
getAddLine({ x: wallBaseLine.x2, y: newPEnd.y }, { x: wallBaseLine.x2, y: wallBaseLine.y2 })
//추가 라인?
findPoints.push({ x: wallBaseLine.x2, y: wallBaseLine.y2 });
} else if (wallBaseLine.x1 <= newPStart.x && newPStart.x <= wallBaseLine.x2 && wallBaseLine.x2 <= newPEnd.x) { //아래 왼쪽
} else if (wallBaseLine.x1 <= newPStart.x && newPStart.x <= wallBaseLine.x2 && wallBaseLine.x2 <= newPEnd.x) { //bottom right
newPStart.x = wallBaseLine.x1;
getAddLine({ x: wallBaseLine.x1, y: newPEnd.y }, { x: wallBaseLine.x1, y: wallBaseLine.y1 })
//추가 라인?
findPoints.push({ x: wallBaseLine.x1, y: wallBaseLine.y1 });
} else if (wallBaseLine.x2 <= newPEnd.x && newPEnd.x <= newPStart.x && newPStart.x <= wallBaseLine.x1) { // top center
newPEnd.x = wallBaseLine.x2;
@ -932,7 +941,7 @@ if(roof.moveUpDown??0 > 0) {
} else if (movedStart) { //end 변경경
if (getOrientation(roofLine) === 'vertical') {
if (getOrientation(roofLine) === 'vertical') { //green 수직
let isCross = false
if (Math.abs(currentRoofLine.x2 - roofLine.x1) < 0.1 || Math.abs(currentRoofLine.x1 - roofLine.x2) < 0.1) {
@ -954,7 +963,7 @@ if(roof.moveUpDown??0 > 0) {
//대각선 라인을 보조라인으로 그린다.
if(isCross){
getAddLine({ x: roofLine.x1, y: currentRoofLine.y1 }, { x: wallBaseLine.x1, y: wallBaseLine.y1 }, 'yellow')
getAddLine({ x: wallBaseLine.x1, y: wallBaseLine.y1 }, { x: roofLine.x1, y: currentRoofLine.y1 }, 'yellow')
}
}else if(newPStart.y <= wallBaseLine.y1 && wallBaseLine.y1 <= newPEnd.y && newPEnd.y <= wallBaseLine.y2) {//상단 오르쪽
@ -962,11 +971,17 @@ if(roof.moveUpDown??0 > 0) {
newPStart = { x: roofLine.x1, y: roofLine.y1 }
newPEnd = { x: roofLine.x2, y: (isCross) ? currentRoofLine.y1 : wallBaseLine.y1 }
}else if(wallBaseLine.y1 <= newPStart.y && newPStart.y <= wallBaseLine.y2 && wallBaseLine.y2 <= newPEnd.y) { //하단 오른쪽v
}else if(wallBaseLine.y1 <= newPStart.y && newPStart.y <= wallBaseLine.y2 && wallBaseLine.y2 <= newPEnd.y) { // bottom left
newPStart = { x: roofLine.x1, y: (isCross) ? currentRoofLine.y1 : wallBaseLine.y1 }
newPEnd = { x: roofLine.x2, y: roofLine.y2 }
//대각선 라인을 보조라인으로 그린다.
if(isCross){
getAddLine( { x: wallBaseLine.x1, y: wallBaseLine.y1 }, { x: roofLine.x1, y: currentRoofLine.y1 }, 'yellow')
}
}else if (wallBaseLine.y2 <= newPEnd.y && newPEnd.y <= wallBaseLine.y1 && wallBaseLine.y1 <= newPStart.y) { //하단 왼쪽
newPStart = { x: roofLine.x1, y: (isCross) ? currentRoofLine.y1 : wallBaseLine.y1 }
@ -975,7 +990,7 @@ if(roof.moveUpDown??0 > 0) {
} else if (getOrientation(roofLine) === 'horizontal') {
} else if (getOrientation(roofLine) === 'horizontal') { //green 수평
let isCross = false
if (Math.abs(currentRoofLine.y1 - roofLine.y2) < 0.1 || Math.abs(currentRoofLine.y2 - roofLine.y1) < 0.1) {
@ -985,25 +1000,37 @@ if(roof.moveUpDown??0 > 0) {
newPStart = { y: roofLine.y1, x: roofLine.x1 }
newPEnd = { y: roofLine.y2, x: (isCross) ? currentRoofLine.x1 : wallBaseLine.x1 }
}else if(newPEnd.x <= wallBaseLine.x2 && wallBaseLine.x2 < wallBaseLine.x1 && wallBaseLine.x1 <= newPStart.x){ //하단 오른쪽v
}else if(newPEnd.x <= wallBaseLine.x2 && wallBaseLine.x2 < wallBaseLine.x1 && wallBaseLine.x1 <= newPStart.x){ //top right
newPStart = { y: roofLine.y1, x: roofLine.x1 }
newPEnd = { y: roofLine.y2, x: (isCross) ? currentRoofLine.x1 : wallBaseLine.x1 }
}else if(newPEnd.x <= wallBaseLine.x2 && wallBaseLine.x2 <= newPStart.x && newPStart.x <= wallBaseLine.x1) { //상단 왼쪽v
newPStart = { y: roofLine.y1, x: (isCross) ? currentRoofLine.x1 : wallBaseLine.x1 }
newPEnd ={ y: roofLine.y2, x: roofLine.x2 }
//대각선 라인을 보조라인으로 그린다.
if(isCross){
getAddLine({ x: currentRoofLine.x1, y: roofLine.y1 }, { x: wallBaseLine.x1, y: wallBaseLine.y1 }, 'yellow')
}
}else if(newPStart.x <= wallBaseLine.x1 && wallBaseLine.x1 <= newPEnd.x && newPEnd.x <= wallBaseLine.x2) {//상단 오르쪽
newPStart = { y: roofLine.y1, x: (isCross) ? currentRoofLine.x1 : wallBaseLine.x1 }
newPEnd ={ y: roofLine.y2, x: roofLine.x2 }
}else if(wallBaseLine.x1 <= newPStart.x && newPStart.x <= wallBaseLine.x2 && wallBaseLine.x2 <= newPEnd.x) { //하단 오른쪽v
}else if(wallBaseLine.x1 <= newPStart.x && newPStart.x <= wallBaseLine.x2 && wallBaseLine.x2 <= newPEnd.x) { //bottom rightv
newPStart = { y: roofLine.y1, x: (isCross) ? currentRoofLine.y1 : wallBaseLine.x1 }
newPStart = { y: roofLine.y1, x: (isCross) ? currentRoofLine.x1 : wallBaseLine.y1 }
newPEnd = { y: roofLine.y2, x: roofLine.x2 }
//대각선 라인을 보조라인으로 그린다.
if(isCross){
getAddLine({ x: wallBaseLine.x1, y: wallBaseLine.y1 },{ x: currentRoofLine.x1, y: roofLine.y1 }, 'yellow')
}
}else if (wallBaseLine.x2 <= newPEnd.x && newPEnd.x <= wallBaseLine.x1 && wallBaseLine.x1 <= newPStart.x) { //right / top
newPStart = { y: roofLine.y1, x: roofLine.x1 }
@ -1037,18 +1064,19 @@ if(roof.moveUpDown??0 > 0) {
newPStart = { x: roofLine.x1, y: (isCross) ? currentRoofLine.y2 : wallBaseLine.y2 }
newPEnd = { x: roofLine.x2, y: roofLine.y2 }
}else if(newPEnd.y <= wallBaseLine.y2 && wallBaseLine.y2 < wallBaseLine.y1 && wallBaseLine.y1 <= newPStart.y){ //top /right
newPStart = { x: roofLine.x1, y: (isCross) ? currentRoofLine.y2 : wallBaseLine.y2 }
newPEnd = { x: roofLine.x2, y: roofLine.y2 }
}else if(newPEnd.y <= wallBaseLine.y2 && wallBaseLine.y2 <= newPStart.y && newPStart.y <= wallBaseLine.y1) { //top / left
newPStart = { x: roofLine.x1, y: (isCross) ? currentRoofLine.y2 : wallBaseLine.y2 }
newPEnd ={ x: roofLine.x2, y: roofLine.y2 }
}else if(newPStart.y <= wallBaseLine.y1 && wallBaseLine.y1 <= newPEnd.y && newPEnd.y <= wallBaseLine.y2) {//top / righty 오르쪽v
}else if(newPStart.y <= wallBaseLine.y1 && wallBaseLine.y1 <= newPEnd.y && newPEnd.y <= wallBaseLine.y2) {//top left / top / righty 오르쪽v
newPStart = { x: roofLine.x1, y: roofLine.y1 }
newPEnd = { x: roofLine.x2, y: (isCross) ? currentRoofLine.y2 : wallBaseLine.y2 }
@ -1063,10 +1091,15 @@ if(roof.moveUpDown??0 > 0) {
newPStart = { x: roofLine.x1, y: (isCross) ? currentRoofLine.y1 : wallBaseLine.y1 }
newPEnd = { x: roofLine.x2, y: roofLine.y2 }
}else if (wallBaseLine.y2 <= newPEnd.y && newPEnd.y <= wallBaseLine.y1 && wallBaseLine.y1 <= newPStart.y) { //하단 왼쪽
}else if (wallBaseLine.y2 <= newPEnd.y && newPEnd.y <= wallBaseLine.y1 && wallBaseLine.y1 <= newPStart.y) { //bottom right
newPStart = { x: roofLine.x1, y: roofLine.y1 }
newPEnd = { x: roofLine.x2, y: (isCross) ? currentRoofLine.y2 : wallBaseLine.y2 }
//대각선 라인을 보조라인으로 그린다.
if(isCross){
getAddLine({ x: roofLine.x2, y: currentRoofLine.y2 }, { x: wallBaseLine.x2, y: wallBaseLine.y2 }, 'yellow')
}
}
@ -1091,11 +1124,17 @@ if(roof.moveUpDown??0 > 0) {
newPStart = { y: roofLine.y1, x: (isCross) ? currentRoofLine.x2 : wallBaseLine.x2 }
newPEnd ={ y: roofLine.y2, x: roofLine.x2 }
}else if(newPStart.x <= wallBaseLine.x1 && wallBaseLine.x1 <= newPEnd.x && newPEnd.x <= wallBaseLine.x2) {//상단 오르쪽v
}else if(newPStart.x <= wallBaseLine.x1 && wallBaseLine.x1 <= newPEnd.x && newPEnd.x <= wallBaseLine.x2) {//bottom left
newPStart = { y: roofLine.y1, x: roofLine.x1 }
newPEnd = { y: roofLine.y2, x: (isCross) ? currentRoofLine.x2 : wallBaseLine.x2 }
//대각선 라인을 보조라인으로 그린다.
if(isCross){
getAddLine({ x: currentRoofLine.x2, y: roofLine.y2 }, { x:wallBaseLine.x2 , y: wallBaseLine.y2 }, 'yellow')
}
}else if(wallBaseLine.x1 <= newPStart.x && newPStart.x <= wallBaseLine.x2 && wallBaseLine.x2 <= newPEnd.x) { //하단 오른쪽v
newPStart = { y: roofLine.y1, x: (isCross) ? currentRoofLine.x1 : wallBaseLine.x1 }
@ -1106,6 +1145,11 @@ if(roof.moveUpDown??0 > 0) {
newPStart = { y: roofLine.y1, x: roofLine.x1 }
newPEnd = { y: roofLine.y2, x: (isCross) ? currentRoofLine.x2 : wallBaseLine.x2 }
//대각선 라인을 보조라인으로 그린다.
if(isCross){
getAddLine({ x: currentRoofLine.x2, y: roofLine.y2 }, { x:wallBaseLine.x2 , y: wallBaseLine.y2 }, 'yellow')
}
}else if (wallBaseLine.x2 <= newPEnd.x && newPEnd.x <= newPStart.x && newPStart.x <= wallBaseLine.x1){ //top center
newPStart = { y: roofLine.y1, x: roofLine.x1 }
newPEnd = { y: roofLine.y2, x: (isCross) ? currentRoofLine.x2 : wallBaseLine.x2 }
@ -3860,35 +3904,51 @@ function updateAndAddLine(innerLines, targetPoint) {
line.y2 === foundLine.y2)
);
let isCurrentLine = false;
if(foundLine.y1 <= targetPoint.y && targetPoint.y <= foundLine.y2){
isCurrentLine = true;
}
const updatedLine = {
...foundLine,
left: (isCurrentLine)?targetPoint.x:foundLine.left,
top: (isCurrentLine)?targetPoint.y:foundLine.top,
x1: (isCurrentLine)?targetPoint.x:foundLine.x1,
y1: (isCurrentLine)?targetPoint.y:foundLine.y1,
x2: (isCurrentLine)?foundLine.x2:targetPoint.x,
y2: (isCurrentLine)?foundLine.y2:targetPoint.y,
startPoint: (isCurrentLine)?{ x: targetPoint.x, y: targetPoint.y }:foundLine.startPoint,
endPoint: (isCurrentLine)?foundLine.endPoint : { x: targetPoint.x, y: targetPoint.y}
};
// Calculate distances to both endpoints
const distanceToStart = Math.hypot(
targetPoint.x - foundLine.x1,
targetPoint.y - foundLine.y1
);
const distanceToEnd = Math.hypot(
targetPoint.x - foundLine.x2,
targetPoint.y - foundLine.y2
);
// Determine which endpoint is closer to the target point
const isUpdatingStart = distanceToStart < distanceToEnd;
const updatedLine = {
...foundLine,
// 수정된 부분: left와 top을 시작점(x1, y1)으로 설정
left: isUpdatingStart ? foundLine.x1 : foundLine.x2,
top: isUpdatingStart ? foundLine.y1 : foundLine.y2,
// 나머지 속성들은 그대로 유지
x1: isUpdatingStart ? targetPoint.x : foundLine.x1,
y1: isUpdatingStart ? targetPoint.y : foundLine.y1,
x2: isUpdatingStart ? foundLine.x2 : targetPoint.x,
y2: isUpdatingStart ? foundLine.y2 : targetPoint.y,
startPoint: {
x: isUpdatingStart ? targetPoint.x : foundLine.x1,
y: isUpdatingStart ? targetPoint.y : foundLine.y1
},
endPoint: {
x: isUpdatingStart ? foundLine.x2 : targetPoint.x,
y: isUpdatingStart ? foundLine.y2 : targetPoint.y
}
};
// 4. If it's a Fabric.js object, use set method if available
if (typeof foundLine.set === 'function') {
foundLine.set({
x1: (isCurrentLine)?targetPoint.x:foundLine.x1,
y1: (isCurrentLine)?targetPoint.y:foundLine.y1,
x2: (isCurrentLine)?foundLine.x2:targetPoint.x,
y2: (isCurrentLine)?foundLine.y2:targetPoint.y,
x1: isUpdatingStart ? targetPoint.x : foundLine.x1,
y1: isUpdatingStart ? targetPoint.y : foundLine.y1,
x2: isUpdatingStart ? foundLine.x2 : targetPoint.x,
y2: isUpdatingStart ? foundLine.y2 : targetPoint.y
});
updatedLines.push(foundLine);
} else {
updatedLines.push(updatedLine);
}
return updatedLines;
}