Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: rectangle subtract func (#1647)
* fix: rectangle subtract func * fix(render): migrate the subtract * fix((render): type error * feat: cr revise --------- Co-authored-by: DR-Univer <wbfsa@qq.com>
- Loading branch information
Showing
4 changed files
with
175 additions
and
15 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Copyright 2023-present DreamNum Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { Rectangle } from '../rectangle'; | ||
|
||
describe('test "Rectangle"', () => { | ||
it('test "subtract"', () => { | ||
// completely covered | ||
const rect1 = { | ||
startRow: 1, | ||
startColumn: 1, | ||
endRow: 1, | ||
endColumn: 1, | ||
}; | ||
|
||
const rect2 = { | ||
startRow: 1, | ||
startColumn: 1, | ||
endRow: 1, | ||
endColumn: 1, | ||
}; | ||
expect(Rectangle.subtract(rect1, rect2)).toEqual([]); | ||
|
||
// partly covered | ||
const rect3 = { | ||
startRow: 1, | ||
startColumn: 1, | ||
endRow: 3, | ||
endColumn: 3, | ||
}; | ||
|
||
const rect4 = { | ||
startRow: 1, | ||
startColumn: 1, | ||
endRow: 1, | ||
endColumn: 1, | ||
}; | ||
expect(Rectangle.subtract(rect3, rect4)).toStrictEqual([ | ||
{ startRow: 2, startColumn: 1, endRow: 3, endColumn: 3 }, | ||
{ startRow: 1, startColumn: 2, endRow: 1, endColumn: 3 }, | ||
]); | ||
|
||
// covered at center point | ||
const rect5 = { | ||
startRow: 1, | ||
startColumn: 1, | ||
endRow: 3, | ||
endColumn: 3, | ||
}; | ||
|
||
const rect6 = { | ||
startRow: 2, | ||
startColumn: 2, | ||
endRow: 2, | ||
endColumn: 2, | ||
}; | ||
|
||
expect(Rectangle.subtract(rect5, rect6)).toStrictEqual([ | ||
{ startRow: 1, startColumn: 1, endRow: 1, endColumn: 3 }, | ||
{ startRow: 3, startColumn: 1, endRow: 3, endColumn: 3 }, | ||
{ startRow: 2, startColumn: 1, endRow: 2, endColumn: 1 }, | ||
{ startRow: 2, startColumn: 3, endRow: 2, endColumn: 3 }, | ||
]); | ||
}); | ||
}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* Copyright 2023-present DreamNum Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import type { IRange } from '@univerjs/core'; | ||
|
||
export function subtractViewportRange(range1: IRange, range2: IRange): IRange[] { | ||
// If there is no intersection, return range1. | ||
|
||
if ( | ||
range2.startRow >= range1.endRow || | ||
range2.endRow <= range1.startRow || | ||
range2.startColumn >= range1.endColumn || | ||
range2.endColumn <= range1.startColumn | ||
) { | ||
return [range1]; | ||
} | ||
|
||
const ranges: IRange[] = []; | ||
|
||
// top | ||
if (range2.startRow > range1.startRow) { | ||
ranges.push({ | ||
startRow: range1.startRow, | ||
startColumn: range1.startColumn, | ||
endRow: range2.startRow, | ||
endColumn: range1.endColumn, | ||
}); | ||
} | ||
|
||
// bottom | ||
if (range2.endRow < range1.endRow) { | ||
ranges.push({ | ||
startRow: range2.endRow, | ||
startColumn: range1.startColumn, | ||
endRow: range1.endRow, | ||
endColumn: range1.endColumn, | ||
}); | ||
} | ||
|
||
// left | ||
const topBoundary = Math.max(range1.startRow, range2.startRow); | ||
const bottomBoundary = Math.min(range1.endRow, range2.endRow); | ||
|
||
if (range2.startColumn > range1.startColumn) { | ||
ranges.push({ | ||
startRow: topBoundary, | ||
startColumn: range1.startColumn, | ||
endRow: bottomBoundary, | ||
endColumn: range2.startColumn, | ||
}); | ||
} | ||
|
||
// right | ||
if (range2.endColumn < range1.endColumn) { | ||
ranges.push({ | ||
startRow: topBoundary, | ||
startColumn: range2.endColumn, | ||
endRow: bottomBoundary, | ||
endColumn: range1.endColumn, | ||
}); | ||
} | ||
|
||
return ranges; | ||
} |
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