|
@@ -181,6 +181,7 @@ export class MoveService implements OnModuleInit {
|
|
|
user.appId = app_id;
|
|
|
user.userId = userId;
|
|
|
user.breakPointId = Number(this.configService.get('app.startPoint')) || 0;
|
|
|
+ console.log('user-init', user);
|
|
|
this.users[userId] = user;
|
|
|
}
|
|
|
|
|
@@ -384,6 +385,9 @@ export class MoveService implements OnModuleInit {
|
|
|
moveFrame.file_name +
|
|
|
'?m=' +
|
|
|
new Date().getTime();
|
|
|
+ if (moveFrame.file_name == '6_7_0.0031.h264') {
|
|
|
+ debugger;
|
|
|
+ }
|
|
|
reply.startBreakPointId = startBreakPointId;
|
|
|
reply.endBreakPointId = endBreakPointId;
|
|
|
replys.push(reply);
|
|
@@ -639,67 +643,79 @@ export class MoveService implements OnModuleInit {
|
|
|
|
|
|
//方向不对的话,人物不能走动,只是修改朝向
|
|
|
// 找到和角度最贴近的邻居点
|
|
|
+ let _angle = this.getAngle(
|
|
|
+ breakPoint.position,
|
|
|
+ {
|
|
|
+ x: this.breakPointInfo[surroundPointIds[0]].position.x,
|
|
|
+ y: this.breakPointInfo[surroundPointIds[0]].position.y,
|
|
|
+ },
|
|
|
+ playerPosition,
|
|
|
+ );
|
|
|
|
|
|
- let _angle = null;
|
|
|
if (surroundPointIds.length == 1) {
|
|
|
- _angle = this.getAngle(
|
|
|
- breakPoint.position,
|
|
|
- {
|
|
|
- x: this.breakPointInfo[surroundPointIds[0]].position.x,
|
|
|
- y: this.breakPointInfo[surroundPointIds[0]].position.y,
|
|
|
- },
|
|
|
+ return await this.moveDirect(
|
|
|
playerPosition,
|
|
|
+ closestDis,
|
|
|
+ breakPointId,
|
|
|
+ surroundPointIds[0],
|
|
|
+ appId,
|
|
|
+ userId,
|
|
|
+ traceId,
|
|
|
+ actionType,
|
|
|
);
|
|
|
- console.log(
|
|
|
- 'joystick:->' +
|
|
|
- 'playerPosition:' +
|
|
|
- JSON.stringify(
|
|
|
- this.reply['newUserStates'][0].playerState.player.position,
|
|
|
- ) +
|
|
|
- ',cameraPosition:' +
|
|
|
- JSON.stringify(
|
|
|
- this.reply['newUserStates'][0].playerState.camera.position,
|
|
|
- ),
|
|
|
- );
|
|
|
- if (_angle > -90 && _angle < 90) {
|
|
|
- return this.reply;
|
|
|
- } else {
|
|
|
- //投影
|
|
|
- console.error('补充!');
|
|
|
- return this.reply;
|
|
|
- }
|
|
|
}
|
|
|
-
|
|
|
//判断_angle与angle的差在45以内的个数,两个的话表示可以走
|
|
|
let count = 0;
|
|
|
const neighPoints = [];
|
|
|
- _angle = this.getAngle(
|
|
|
- breakPoint.position,
|
|
|
- { x: breakPoint.position.x + 1, y: breakPoint.position.y },
|
|
|
- playerPosition,
|
|
|
- );
|
|
|
- if (_angle < 0) {
|
|
|
- _angle += 360;
|
|
|
- }
|
|
|
+ // _angle = this.getAngle(
|
|
|
+ // user.player.position,
|
|
|
+ // { x: user.player.position.x + 1, y: user.player.position.y },
|
|
|
+ // playerPosition,
|
|
|
+ // );
|
|
|
+ // if (_angle < 0) {
|
|
|
+ // _angle += 360;
|
|
|
+ // }
|
|
|
|
|
|
for (let i = 0; i < surroundPointIds.length; ++i) {
|
|
|
const neighPoint = this.breakPointInfo[surroundPointIds[i]];
|
|
|
neighPoint.breakPointId = surroundPointIds[i];
|
|
|
- // const neighDistance = this.getDistance(
|
|
|
- // playerPosition,
|
|
|
- // neighPoint.position,
|
|
|
- // );
|
|
|
- // if (neighDistance < closestDis) {
|
|
|
- // chooseBreakPointId = surroundPointIds[i];
|
|
|
- // }
|
|
|
angle = this.getAngle(
|
|
|
breakPoint.position,
|
|
|
{ x: breakPoint.position.x + 1, y: breakPoint.position.y },
|
|
|
+ //playerPosition,
|
|
|
neighPoint.position,
|
|
|
);
|
|
|
if (angle < 0) {
|
|
|
angle += 360;
|
|
|
}
|
|
|
+
|
|
|
+ //if(angle<45&&angle!=0){
|
|
|
+ if (
|
|
|
+ Math.abs(angle - move_angle) < 45 &&
|
|
|
+ Math.abs(angle - move_angle) != 0
|
|
|
+ ) {
|
|
|
+ neighPoint.angle = angle;
|
|
|
+ neighPoints.push(neighPoint);
|
|
|
+ ++count;
|
|
|
+ } else if (Math.abs(angle - move_angle) == 0) {
|
|
|
+ return await this.moveDirect(
|
|
|
+ playerPosition,
|
|
|
+ closestDis,
|
|
|
+ breakPointId,
|
|
|
+ surroundPointIds[i],
|
|
|
+ appId,
|
|
|
+ userId,
|
|
|
+ traceId,
|
|
|
+ actionType,
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if (angle == 0 && Math.abs(360 - move_angle) < 45) {
|
|
|
+ neighPoint.angle = angle;
|
|
|
+ neighPoints.push(neighPoint);
|
|
|
+ ++count;
|
|
|
+ }
|
|
|
+ /*
|
|
|
//neighAngles.push(angle);
|
|
|
if (Math.abs(angle - _angle) < 45) {
|
|
|
neighPoint.angle = angle;
|
|
@@ -712,32 +728,66 @@ export class MoveService implements OnModuleInit {
|
|
|
} else if (Math.abs(angle - _angle) == 0) {
|
|
|
neighPoint.angle = angle;
|
|
|
neighPoints.push(neighPoint);
|
|
|
+ ++count;
|
|
|
}
|
|
|
+ */
|
|
|
}
|
|
|
|
|
|
- if (count == 2 || _angle % 45 == 0) {
|
|
|
+ if (count == 2) {
|
|
|
//人物移动
|
|
|
user.player.position = JSON.parse(JSON.stringify(playerPosition));
|
|
|
this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
|
|
|
JSON.stringify(playerPosition),
|
|
|
);
|
|
|
- } else {
|
|
|
- console.log(
|
|
|
- 'joystick:->' +
|
|
|
- 'playerPosition:' +
|
|
|
- JSON.stringify(
|
|
|
- this.reply['newUserStates'][0].playerState.player.position,
|
|
|
- ) +
|
|
|
- ',cameraPosition:' +
|
|
|
- JSON.stringify(
|
|
|
- this.reply['newUserStates'][0].playerState.camera.position,
|
|
|
- ),
|
|
|
+ } else if (count == 1) {
|
|
|
+ return await this.moveDirect(
|
|
|
+ playerPosition,
|
|
|
+ closestDis,
|
|
|
+ breakPointId,
|
|
|
+ neighPoints[0].breakPointId,
|
|
|
+ appId,
|
|
|
+ userId,
|
|
|
+ traceId,
|
|
|
+ actionType,
|
|
|
+ );
|
|
|
+ // //投影
|
|
|
+ // let player_Position = this.getTarget(
|
|
|
+ // playerPosition,
|
|
|
+ // breakPoint.position,
|
|
|
+ // neighPoints[0].position,
|
|
|
+ // );
|
|
|
+ // if (player_Position != null) {
|
|
|
+ // playerPosition.x = player_Position.x;
|
|
|
+ // player_Position.y = player_Position.y;
|
|
|
+ // user.player.position = JSON.parse(JSON.stringify(playerPosition));
|
|
|
+ // }
|
|
|
+
|
|
|
+ // if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
|
|
|
+ // debugger;
|
|
|
+ // return await this.moveCamera(breakPointId,neighPoints[0].breakPointId,appId,userId,traceId,actionType);
|
|
|
+ // }
|
|
|
+ // else{
|
|
|
+ // this.reply['newUserStates'][0].playerState.player.position = JSON.parse(JSON.stringify(user.player.position));
|
|
|
+ // return this.reply;
|
|
|
+ // }
|
|
|
+ } else if (count == 0) {
|
|
|
+ this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
|
|
|
+ JSON.stringify(user.player.position),
|
|
|
);
|
|
|
return this.reply;
|
|
|
}
|
|
|
|
|
|
+ //count == 2
|
|
|
//超出范围了
|
|
|
if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
|
|
|
+ _angle = this.getAngle(
|
|
|
+ breakPoint.position,
|
|
|
+ {
|
|
|
+ x: breakPoint.position.x + 1,
|
|
|
+ y: breakPoint.position.y,
|
|
|
+ },
|
|
|
+ playerPosition,
|
|
|
+ );
|
|
|
//角度是45的倍数
|
|
|
if (_angle % 45 == 0) {
|
|
|
chooseBreakPointId = neighPoints[0].breakPointId;
|
|
@@ -751,79 +801,21 @@ export class MoveService implements OnModuleInit {
|
|
|
} else {
|
|
|
chooseBreakPointId = neighPoints[0].breakPointId;
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- if (chooseBreakPointId == null) {
|
|
|
- console.log(
|
|
|
- 'joystick:->' +
|
|
|
- 'playerPosition:' +
|
|
|
- JSON.stringify(
|
|
|
- this.reply['newUserStates'][0].playerState.player.position,
|
|
|
- ) +
|
|
|
- ',cameraPosition:' +
|
|
|
- JSON.stringify(
|
|
|
- this.reply['newUserStates'][0].playerState.camera.position,
|
|
|
- ),
|
|
|
- );
|
|
|
- return this.reply;
|
|
|
- }
|
|
|
- //判断人物离该邻接点的距离是否在最小路径内,如果是,跳到这个邻接点里
|
|
|
- //相机纠正加过渡
|
|
|
- else {
|
|
|
- if (chooseBreakPointId == user.breakPointId) {
|
|
|
- console.log(
|
|
|
- 'joystick:->' +
|
|
|
- 'playerPosition:' +
|
|
|
- JSON.stringify(
|
|
|
- this.reply['newUserStates'][0].playerState.player.position,
|
|
|
- ) +
|
|
|
- ',cameraPosition:' +
|
|
|
- JSON.stringify(
|
|
|
- this.reply['newUserStates'][0].playerState.camera.position,
|
|
|
- ),
|
|
|
- );
|
|
|
- return this.reply;
|
|
|
- }
|
|
|
- angle = user.camera.angle.yaw % 45; //纠正需要
|
|
|
- //通过user.camera.angle矫正相机的angle
|
|
|
- const checkReplys = await this.modeifyCameraAngle(
|
|
|
- angle,
|
|
|
+ return await this.moveCamera(
|
|
|
+ breakPointId,
|
|
|
+ chooseBreakPointId,
|
|
|
+ appId,
|
|
|
userId,
|
|
|
traceId,
|
|
|
actionType,
|
|
|
);
|
|
|
- console.log('joystick:过渡');
|
|
|
- replys.push(checkReplys);
|
|
|
-
|
|
|
- //读redis里的数据,按照frame_index的大小排序
|
|
|
- const key =
|
|
|
- 'moveframe:app_id:' +
|
|
|
- appId +
|
|
|
- ':start_break_point_id:' +
|
|
|
- breakPointId +
|
|
|
- ':end_break_point_id:' +
|
|
|
- chooseBreakPointId +
|
|
|
- ':angle:' +
|
|
|
- Math.floor(user.camera.angle.yaw / 45);
|
|
|
- const moveFramesRes = await this.cacheService.get(key);
|
|
|
- const moveFrames = JSON.parse(moveFramesRes);
|
|
|
- const pathReplys = this.createCacheReplys(
|
|
|
- appId,
|
|
|
- moveFrames,
|
|
|
- traceId,
|
|
|
- userId,
|
|
|
- breakPointId,
|
|
|
- chooseBreakPointId,
|
|
|
- true,
|
|
|
- );
|
|
|
- replys.push(pathReplys);
|
|
|
- user.breakPointId = chooseBreakPointId;
|
|
|
- const lastFrame = moveFrames[moveFrames.length - 1];
|
|
|
- user.camera.position = JSON.parse(
|
|
|
- JSON.stringify(lastFrame.camera_position),
|
|
|
+ } else {
|
|
|
+ console.log('joystick-1:');
|
|
|
+ user.player.position = JSON.parse(JSON.stringify(playerPosition));
|
|
|
+ this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
|
|
|
+ JSON.stringify(user.player.position),
|
|
|
);
|
|
|
- user.camera.angle.yaw = lastFrame.camera_angle.yaw;
|
|
|
- return replys;
|
|
|
+ return this.reply;
|
|
|
}
|
|
|
} catch (error) {
|
|
|
console.log('MoveService', error);
|
|
@@ -832,6 +824,118 @@ export class MoveService implements OnModuleInit {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ async moveDirect(
|
|
|
+ playerPosition,
|
|
|
+ closestDis,
|
|
|
+ breakPointId,
|
|
|
+ neighBreakPointId,
|
|
|
+ appId,
|
|
|
+ userId,
|
|
|
+ traceId,
|
|
|
+ actionType,
|
|
|
+ ) {
|
|
|
+ const breakPoint = this.breakPointInfo[breakPointId];
|
|
|
+ const user = this.users[userId];
|
|
|
+ const player_Position = this.getTarget(
|
|
|
+ playerPosition,
|
|
|
+ breakPoint.position,
|
|
|
+ //this.breakPointInfo[surroundPointIds[0]].position,
|
|
|
+ this.breakPointInfo[neighBreakPointId].position,
|
|
|
+ );
|
|
|
+ if (player_Position != null) {
|
|
|
+ playerPosition.x = player_Position.x;
|
|
|
+ playerPosition.y = player_Position.y;
|
|
|
+ user.player.position = JSON.parse(JSON.stringify(playerPosition));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
|
|
|
+ //return await this.moveCamera(breakPointId,surroundPointIds[0],appId,userId,traceId,actionType);
|
|
|
+ return await this.moveCamera(
|
|
|
+ breakPointId,
|
|
|
+ neighBreakPointId,
|
|
|
+ appId,
|
|
|
+ userId,
|
|
|
+ traceId,
|
|
|
+ actionType,
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
|
|
|
+ JSON.stringify(user.player.position),
|
|
|
+ );
|
|
|
+ return this.reply;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async moveCamera(
|
|
|
+ breakPointId,
|
|
|
+ chooseBreakPointId,
|
|
|
+ appId,
|
|
|
+ userId,
|
|
|
+ traceId,
|
|
|
+ actionType,
|
|
|
+ ) {
|
|
|
+ const user = this.users[userId];
|
|
|
+ if (chooseBreakPointId == null) {
|
|
|
+ this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
|
|
|
+ JSON.stringify(user.player.position),
|
|
|
+ );
|
|
|
+ console.log('joystick-2:');
|
|
|
+ return this.reply;
|
|
|
+ }
|
|
|
+ //判断人物离该邻接点的距离是否在最小路径内,如果是,跳到这个邻接点里
|
|
|
+ //相机纠正加过渡
|
|
|
+ else {
|
|
|
+ if (chooseBreakPointId == user.breakPointId) {
|
|
|
+ console.log('joystick-3:');
|
|
|
+ this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
|
|
|
+ JSON.stringify(user.player.position),
|
|
|
+ );
|
|
|
+ return this.reply;
|
|
|
+ }
|
|
|
+ const replys = [];
|
|
|
+ const angle = user.camera.angle.yaw % 45; //纠正需要
|
|
|
+ //通过user.camera.angle矫正相机的angle
|
|
|
+ const checkReplys = await this.modeifyCameraAngle(
|
|
|
+ angle,
|
|
|
+ userId,
|
|
|
+ traceId,
|
|
|
+ actionType,
|
|
|
+ );
|
|
|
+ replys.push(checkReplys);
|
|
|
+
|
|
|
+ //读redis里的数据,按照frame_index的大小排序
|
|
|
+ const key =
|
|
|
+ 'moveframe:app_id:' +
|
|
|
+ appId +
|
|
|
+ ':start_break_point_id:' +
|
|
|
+ breakPointId +
|
|
|
+ ':end_break_point_id:' +
|
|
|
+ chooseBreakPointId +
|
|
|
+ ':angle:' +
|
|
|
+ Math.floor(user.camera.angle.yaw / 45);
|
|
|
+ const moveFramesRes = await this.cacheService.get(key);
|
|
|
+ const moveFrames = JSON.parse(moveFramesRes);
|
|
|
+ const pathReplys = this.createCacheReplys(
|
|
|
+ appId,
|
|
|
+ moveFrames,
|
|
|
+ traceId,
|
|
|
+ userId,
|
|
|
+ breakPointId,
|
|
|
+ chooseBreakPointId,
|
|
|
+ true,
|
|
|
+ );
|
|
|
+ replys.push(pathReplys);
|
|
|
+ user.breakPointId = chooseBreakPointId;
|
|
|
+ const lastFrame = moveFrames[moveFrames.length - 1];
|
|
|
+ user.camera.position = JSON.parse(
|
|
|
+ JSON.stringify(lastFrame.camera_position),
|
|
|
+ );
|
|
|
+ user.camera.angle.yaw = lastFrame.camera_angle.yaw;
|
|
|
+ console.log('joystick-4:');
|
|
|
+ return replys;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
async modeifyCameraAngle(angle, userId, traceId, actionType) {
|
|
|
const checkReplys = [];
|
|
|
if (angle > 22) {
|
|
@@ -873,10 +977,187 @@ export class MoveService implements OnModuleInit {
|
|
|
return checkReplys;
|
|
|
}
|
|
|
|
|
|
+ getTarget(position, position1, position2) {
|
|
|
+ const line = this.createLine1(position1, position2);
|
|
|
+ const join = this.getJoinLinePoint(position, line);
|
|
|
+ // if (this.isContainForSegment(join, position1, position2)) {
|
|
|
+ // return join;
|
|
|
+ // } else {
|
|
|
+ // return null;
|
|
|
+ // }
|
|
|
+ return join;
|
|
|
+ }
|
|
|
+
|
|
|
getDistance(position1, position2) {
|
|
|
return Math.sqrt(
|
|
|
(position1.x - position2.x) * (position1.x - position2.x) +
|
|
|
(position1.y - position2.y) * (position1.y - position2.y),
|
|
|
);
|
|
|
}
|
|
|
+
|
|
|
+ isContainForSegment(point, startPoint, endPoint) {
|
|
|
+ const minDis = 0.1;
|
|
|
+ const dis1 =
|
|
|
+ this.getDistance(startPoint, point) + this.getDistance(endPoint, point);
|
|
|
+ const dis2 = this.getDistance(startPoint, endPoint);
|
|
|
+ if (Math.abs(dis1 - dis2) < minDis) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //两条线的交点
|
|
|
+ getIntersectionPoint(parameter1, parameter2) {
|
|
|
+ if (this.isParallel(parameter1, parameter2)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (
|
|
|
+ typeof parameter1.a == 'undefined' &&
|
|
|
+ typeof parameter2.a != 'undefined'
|
|
|
+ ) {
|
|
|
+ if (parameter1.x) {
|
|
|
+ return {
|
|
|
+ x: parameter1.x,
|
|
|
+ y: parameter2.a * parameter1.x + parameter2.b,
|
|
|
+ };
|
|
|
+ } else if (parameter1.y) {
|
|
|
+ return {
|
|
|
+ x: (parameter1.y - parameter2.b) / parameter2.a,
|
|
|
+ y: parameter1.y,
|
|
|
+ };
|
|
|
+ }
|
|
|
+ } else if (
|
|
|
+ typeof parameter2.a == 'undefined' &&
|
|
|
+ typeof parameter1.a != 'undefined'
|
|
|
+ ) {
|
|
|
+ if (parameter2.x) {
|
|
|
+ return {
|
|
|
+ x: parameter2.x,
|
|
|
+ y: parameter1.a * parameter2.x + parameter1.b,
|
|
|
+ };
|
|
|
+ } else if (parameter2.y) {
|
|
|
+ return {
|
|
|
+ x: (parameter2.y - parameter1.b) / parameter1.a,
|
|
|
+ y: parameter2.y,
|
|
|
+ };
|
|
|
+ }
|
|
|
+ } else if (
|
|
|
+ typeof parameter2.a == 'undefined' &&
|
|
|
+ typeof parameter1.a == 'undefined'
|
|
|
+ ) {
|
|
|
+ if (parameter1.hasOwnProperty('x') && parameter2.hasOwnProperty('y')) {
|
|
|
+ return { x: parameter1.x, y: parameter2.y };
|
|
|
+ } else if (
|
|
|
+ parameter1.hasOwnProperty('y') &&
|
|
|
+ parameter2.hasOwnProperty('x')
|
|
|
+ ) {
|
|
|
+ return { x: parameter2.x, y: parameter1.y };
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (parameter1.a == parameter2.a) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ const joinpointx =
|
|
|
+ (parameter2.b - parameter1.b) / (parameter1.a - parameter2.a);
|
|
|
+ const joinpointy =
|
|
|
+ (parameter1.a * parameter2.b - parameter2.a * parameter1.b) /
|
|
|
+ (parameter1.a - parameter2.a);
|
|
|
+
|
|
|
+ const point = { x: joinpointx, y: joinpointy };
|
|
|
+ return point;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 垂直线
|
|
|
+ getVerticalLine(line, point) {
|
|
|
+ if (typeof line.a === 'undefined') {
|
|
|
+ if (line.hasOwnProperty('x')) {
|
|
|
+ return { y: point.y };
|
|
|
+ } else if (line.hasOwnProperty('y')) {
|
|
|
+ return { x: point.x };
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else if (line.a == 0) {
|
|
|
+ return { x: point.x };
|
|
|
+ } else {
|
|
|
+ const tl = {};
|
|
|
+ tl.a = -1 / line.a;
|
|
|
+ const result = this.createLine2(tl, point);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 经过point且与line垂直的直线,该直线与line的交点
|
|
|
+ getJoinLinePoint(point, line) {
|
|
|
+ const verticalLine = this.getVerticalLine(line, point);
|
|
|
+ const join = this.getIntersectionPoint(line, verticalLine);
|
|
|
+ return join;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 与lineA平行并且point在线上
|
|
|
+ createLine2(lineA, point) {
|
|
|
+ const parameter = {};
|
|
|
+ if (typeof lineA.a === 'undefined') {
|
|
|
+ if (typeof lineA.x !== 'undefined') {
|
|
|
+ parameter.x = point.x;
|
|
|
+ } else if (typeof lineA.y !== 'undefined') {
|
|
|
+ parameter.y = point.y;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ parameter.a = lineA.a;
|
|
|
+ parameter.b = point.y - point.x * lineA.a;
|
|
|
+ }
|
|
|
+ return parameter;
|
|
|
+ }
|
|
|
+
|
|
|
+ createLine1(point1, point2) {
|
|
|
+ if (point1.x == point2.x && point1.y == point2.y) {
|
|
|
+ return null;
|
|
|
+ } else if (this.getFixed(Math.abs(point1.x - point2.x)) == 0) {
|
|
|
+ return { x: point1.x };
|
|
|
+ } else if (this.getFixed(Math.abs(point1.y - point2.y)) == 0) {
|
|
|
+ return { y: point1.y };
|
|
|
+ }
|
|
|
+
|
|
|
+ const parametera = (point1.y - point2.y) / (point1.x - point2.x);
|
|
|
+ const parameterb =
|
|
|
+ (point1.x * point2.y - point2.x * point1.y) / (point1.x - point2.x);
|
|
|
+ if (this.getFixed(parametera) == 0) {
|
|
|
+ return { y: this.getFixed(parameterb) };
|
|
|
+ }
|
|
|
+ const parameter = {
|
|
|
+ a: this.getFixed(parametera),
|
|
|
+ b: this.getFixed(parameterb),
|
|
|
+ };
|
|
|
+ return parameter;
|
|
|
+ }
|
|
|
+
|
|
|
+ //返回true表示平行
|
|
|
+ isParallel(line1, line2) {
|
|
|
+ if (typeof line1.a == 'undefined' && typeof line2.a == 'undefined') {
|
|
|
+ if (line1.hasOwnProperty('x') && line2.hasOwnProperty('x')) {
|
|
|
+ return true;
|
|
|
+ } else if (line1.hasOwnProperty('y') && line2.hasOwnProperty('y')) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } else if (typeof line1.a == 'undefined' || typeof line2.a == 'undefined') {
|
|
|
+ return false;
|
|
|
+ } else if (this.getFixed(line1.a) == this.getFixed(line2.a)) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ getFixed(num) {
|
|
|
+ const decimal = 2;
|
|
|
+ return parseFloat(num.toFixed(decimal));
|
|
|
+ }
|
|
|
}
|