完善盒子相交测试和线段相交测试的代码文档.

分离`DoIntersect`代码
pull/166/MERGE
ChenX 6 years ago
parent 3f459abcab
commit 8b262d2c68

@ -2,7 +2,7 @@ import { Frustum, Matrix4, Object3D, OrthographicCamera, PerspectiveCamera, Vect
import { PreViewer } from '../GraphicsSystem/PreViewer';
import { Viewer } from '../GraphicsSystem/Viewer';
import { SelectSetBase } from './SelectBase';
import { doIntersect } from '../Geometry/CheckIntersect';
import { doIntersect } from "../Geometry/DoIntersect";
export class SelectLine extends SelectSetBase
{

@ -1,6 +1,15 @@
import { Box2, Vector2, Math as Math3 } from "three";
import { doIntersect } from "./DoIntersect";
/*
:,()线.
1.使.
:https://zh.wikipedia.org/wiki/%E7%A7%91%E6%81%A9%EF%BC%8D%E8%8B%8F%E6%B3%BD%E5%85%B0%E7%AE%97%E6%B3%95
2.使线.
:doIntersect.
*/
interface Vec2
export interface Vec2
{
x: number;
y: number;
@ -11,23 +20,26 @@ const LEFT = 1, RIGHT = 2, DOWN = 4, TOP = 8;
// bounded diagonally by (xmin, ymin), and (xmax, ymax)
// ASSUME THAT xmax, xmin, ymax and ymin are global constants.
function ComputeOutCode(x: number, y: number, box: Box2): number
{
let code = 0;
if (x < box.min.x) // to the left of clip window
code |= 1;
code |= LEFT;
else if (x > box.max.x) // to the right of clip window
code |= 2;
code |= RIGHT;
if (y < box.min.y) // below the clip window
code |= 4;
code |= DOWN;
else if (y > box.max.y) // above the clip window
code |= 8;
code |= TOP;
return code;
}
/**
* ,线
*/
export class BoxCheckIntersect
{
p1: Vec2;
@ -75,66 +87,3 @@ export class BoxCheckIntersect
return ((nearestX - cen.x) ** 2 + (nearestY - cen.y) ** 2) <= radius ** 2;
}
}
//判断线段是否存在交点 ref: https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/
// Given three colinear points p, q, r, the function checks if
// point q lies on line segment 'pr'
function onSegment(p: Vec2, q: Vec2, r: Vec2): boolean
{
if (q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) &&
q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y))
return true;
return false;
}
// To find orientation of ordered triplet (p, q, r).
// The function returns following values
// 0 --> p, q and r are colinear
// 1 --> Clockwise
// 2 --> Counterclockwise
function orientation(p: Vec2, q: Vec2, r: Vec2): number
{
// See https://www.geeksforgeeks.org/orientation-3-ordered-points/
// for details of below formula.
let val = (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if (val == 0) return 0; // colinear
return (val > 0) ? 1 : 2; // clock or counterclock wise
}
/**
* `p1q1``p2q2`.
*/
export function doIntersect(p1: Vec2, q1: Vec2, p2: Vec2, q2: Vec2): boolean
{
// Find the four orientations needed for general and
// special cases
let o1 = orientation(p1, q1, p2);
let o2 = orientation(p1, q1, q2);
let o3 = orientation(p2, q2, p1);
let o4 = orientation(p2, q2, q1);
// General case
if (o1 != o2 && o3 != o4)
return true;
// Special Cases
// p1, q1 and p2 are colinear and p2 lies on segment p1q1
if (o1 == 0 && onSegment(p1, p2, q1)) return true;
// p1, q1 and q2 are colinear and q2 lies on segment p1q1
if (o2 == 0 && onSegment(p1, q2, q1)) return true;
// p2, q2 and p1 are colinear and p1 lies on segment p2q2
if (o3 == 0 && onSegment(p2, p1, q2)) return true;
// p2, q2 and q1 are colinear and q1 lies on segment p2q2
if (o4 == 0 && onSegment(p2, q1, q2)) return true;
return false; // Doesn't fall in any of the above cases
}

@ -0,0 +1,59 @@
import { Vec2 } from "./CheckIntersect";
/*
:线
ref:https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/
*/
// Given three colinear points p, q, r, the function checks if
// point q lies on line segment 'pr'
function onSegment(p: Vec2, q: Vec2, r: Vec2): boolean
{
if (q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) &&
q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y))
return true;
return false;
}
// To find orientation of ordered triplet (p, q, r).
// The function returns following values
// 0 --> p, q and r are colinear
// 1 --> Clockwise
// 2 --> Counterclockwise
function orientation(p: Vec2, q: Vec2, r: Vec2): number
{
// See https://www.geeksforgeeks.org/orientation-3-ordered-points/
// for details of below formula.
let val = (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if (val == 0)
return 0; // colinear
return (val > 0) ? 1 : 2; // clock or counterclock wise
}
/**
* 线`p1q1`线`p2q2`.
*/
export function doIntersect(p1: Vec2, q1: Vec2, p2: Vec2, q2: Vec2): boolean
{
// Find the four orientations needed for general and
// special cases
let o1 = orientation(p1, q1, p2);
let o2 = orientation(p1, q1, q2);
let o3 = orientation(p2, q2, p1);
let o4 = orientation(p2, q2, q1);
// General case
if (o1 !== o2 && o3 !== o4)
return true;
// Special Cases
// p1, q1 and p2 are colinear and p2 lies on segment p1q1
if (o1 === 0 && onSegment(p1, p2, q1))
return true;
// p1, q1 and q2 are colinear and q2 lies on segment p1q1
if (o2 === 0 && onSegment(p1, q2, q1))
return true;
// p2, q2 and p1 are colinear and p1 lies on segment p2q2
if (o3 === 0 && onSegment(p2, p1, q2))
return true;
// p2, q2 and q1 are colinear and q1 lies on segment p2q2
if (o4 === 0 && onSegment(p2, q1, q2))
return true;
return false; // Doesn't fall in any of the above cases
}
Loading…
Cancel
Save