Skip to content

Commit

Permalink
Merge pull request #217 from VisActor/fix/obb-bounds-clone
Browse files Browse the repository at this point in the history
fix: fix bug of `clone()` in obb
  • Loading branch information
xile611 authored Dec 25, 2024
2 parents 9002e28 + 00ee4bf commit d4cf985
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"comment": "fix: fix bug of `clone()` in obb\n\n",
"type": "none",
"packageName": "@visactor/vutils"
}
],
"packageName": "@visactor/vutils",
"email": "dingling112@gmail.com"
}
23 changes: 23 additions & 0 deletions packages/vutils/__tests__/common/isPlainObject.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isPlainObject } from '../../src';

describe('isPlainObject', () => {
it('isPlainObject({}) should be true', () => {
expect(isPlainObject({})).toBeTruthy();
});

it('isPlainObject([1, 2, 3]) should be false', () => {
expect(isPlainObject([1, 2, 3])).toBeFalsy();
});

it('isPlainObject(Function) should be false', () => {
expect(isPlainObject(Function)).toBeFalsy();
});

it('isPlainObject(null) should be false', () => {
expect(isPlainObject(null)).toBeFalsy();
});

it('isPlainObject(undefined) should be false', () => {
expect(isPlainObject(undefined)).toBeFalsy();
});
});
15 changes: 15 additions & 0 deletions packages/vutils/__tests__/data-structure/obb.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { OBBBounds } from '../../src/index';

describe('OBBBounds', () => {
it('clone of OBBBounds', () => {
const a = new OBBBounds();
a.setValue(10, 10, 100, 100, Math.PI / 4);
const b = a.clone();

expect(a.x1).toBe(b.x1);
expect(a.x2).toBe(b.x2);
expect(a.y1).toBe(b.y1);
expect(a.y2).toBe(b.y2);
expect(a.angle).toBe(b.angle);
});
});
6 changes: 5 additions & 1 deletion packages/vutils/src/data-structure/bounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ export class OBBBounds extends Bounds {
constructor(bounds?: Bounds, angle = 0) {
super(bounds);
if (bounds) {
this.angle = angle;
this.angle = (bounds as any).angle ?? angle;
}
}

Expand All @@ -437,4 +437,8 @@ export class OBBBounds extends Bounds {
this.angle = angle;
return this;
}

clone(): OBBBounds {
return new OBBBounds(this);
}
}
38 changes: 1 addition & 37 deletions packages/vutils/src/graphics/algorithm/intersect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,53 +302,17 @@ function toRect(box: RotateBound, isDeg: boolean) {
)
];
}
export function isRotateAABBIntersect(
box1: RotateBound,
box2: RotateBound,
isDeg = false,
ctx?: CanvasRenderingContext2D
) {
export function isRotateAABBIntersect(box1: RotateBound, box2: RotateBound, isDeg = false) {
const rect1 = toRect(box1, isDeg);
const rect2 = toRect(box2, isDeg);

const vector = (start: Point, end: Point) => {
return [end.x - start.x, end.y - start.y] as [number, number];
};

if (ctx) {
ctx.save();
ctx.fillStyle = 'red';
ctx.globalAlpha = 0.6;
rect1.forEach((item, index) => {
if (index === 0) {
ctx.moveTo(item.x, item.y);
} else {
ctx.lineTo(item.x, item.y);
}
});
ctx.fill();
ctx.restore();

ctx.save();
ctx.fillStyle = 'green';
ctx.globalAlpha = 0.6;
rect2.forEach((item, index) => {
if (index === 0) {
ctx.moveTo(item.x, item.y);
} else {
ctx.lineTo(item.x, item.y);
}
});
ctx.fill();
ctx.restore();
}

// 两个矩形的中心点
const p1 = getCenterPoint(box1);
const p2 = getCenterPoint(box2);

ctx && ctx.fillRect(p1.x, p1.y, 2, 2);
ctx && ctx.fillRect(p2.x, p2.y, 2, 2);
// 向量 p1p2
const vp1p2 = vector(p1, p2);

Expand Down

0 comments on commit d4cf985

Please sign in to comment.