对像素进行取整保证在不同分辨率下保持清晰

This commit is contained in:
yhh
2021-05-25 11:16:49 +08:00
parent da3ab02a8d
commit 6c44d38c10
8 changed files with 53 additions and 45 deletions

View File

@@ -456,6 +456,10 @@ module es {
return t - Math.floor(t / length) * length;
}
public static floorToInt(f: number) {
return Math.trunc(Math.floor(f));
}
/**
* 将值绕一圈移动的助手
* @param position

View File

@@ -411,10 +411,10 @@ module es {
public calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2,
rotation: number, width: number, height: number) {
if (rotation == 0) {
this.x = parentPosition.x + position.x - origin.x * scale.x;
this.y = parentPosition.y + position.y - origin.y * scale.y;
this.width = width * scale.x;
this.height = height * scale.y;
this.x = Math.trunc(parentPosition.x + position.x - origin.x * scale.x);
this.y = Math.trunc(parentPosition.y + position.y - origin.y * scale.y);
this.width = Math.trunc(width * scale.x);
this.height = Math.trunc(height * scale.y);
} else {
// 我们需要找到我们的绝对最小/最大值,并据此创建边界
let worldPosX = parentPosition.x + position.x;
@@ -441,14 +441,14 @@ module es {
Vector2Ext.transformR(bottomRight, this._transformMat, bottomRight);
// 找出最小值和最大值,这样我们就可以计算出我们的边界框。
let minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
let maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
let minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
let maxY = Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
let minX = Math.trunc(Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x));
let maxX = Math.trunc(Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x));
let minY = Math.trunc(Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y));
let maxY = Math.trunc(Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y));
this.location = new Vector2(minX, minY);
this.width = maxX - minX;
this.height = maxY - minY;
this.width = Math.trunc(maxX - minX);
this.height = Math.trunc(maxY - minY);
}
}
@@ -549,7 +549,7 @@ module es {
* 获取这个矩形的哈希码
*/
public getHashCode(): number{
return (this.x ^ this.y ^ this.width ^ this.height);
return (Math.trunc(this.x) ^ Math.trunc(this.y) ^ Math.trunc(this.width) ^ Math.trunc(this.height));
}
public clone(): Rectangle {

View File

@@ -16,7 +16,7 @@ module es {
*/
public update(amount: number){
this.remainder += amount;
let motion = Math.floor(Math.trunc(this.remainder));
let motion = Math.trunc(this.remainder);
this.remainder -= motion;
amount = motion;
return amount;

View File

@@ -181,11 +181,11 @@ module es {
while (currentCell.x != lastCell.x || currentCell.y != lastCell.y){
if (tMaxX < tMaxY){
currentCell.x = Math.floor(MathHelper.approach(currentCell.x, lastCell.x, Math.abs(stepX)));
currentCell.x = Math.trunc(MathHelper.approach(currentCell.x, lastCell.x, Math.abs(stepX)));
tMaxX += tDeltaX;
}else{
currentCell.y = Math.floor(MathHelper.approach(currentCell.y, lastCell.y, Math.abs(stepY)));
currentCell.y = Math.trunc(MathHelper.approach(currentCell.y, lastCell.y, Math.abs(stepY)));
tMaxY += tDeltaY;
}
@@ -286,7 +286,7 @@ module es {
* @param y
*/
public cellCoords(x: number, y: number): Vector2 {
return new Vector2(Math.floor(x * this._inverseCellSize), Math.floor(y * this._inverseCellSize));
return new Vector2(MathHelper.floorToInt(x * this._inverseCellSize), MathHelper.floorToInt(y * this._inverseCellSize));
}
/**

View File

@@ -265,8 +265,8 @@ module es {
public static getClosestPointOnRectangleBorderToPoint(rect: Rectangle, point: Vector2) {
// 对于每个轴,如果该点在盒子外面,则将在盒子上,否则不理会它
let res = es.Vector2.zero;
res.x = MathHelper.clamp(point.x, rect.left, rect.right)
res.y = MathHelper.clamp(point.y, rect.top, rect.bottom);
res.x = MathHelper.clamp(Math.trunc(point.x), rect.left, rect.right)
res.y = MathHelper.clamp(Math.trunc(point.y), rect.top, rect.bottom);
// 如果点在矩形内我们需要将res推到边框因为它将在矩形内
if (rect.contains(res.x, res.y)) {
@@ -327,7 +327,7 @@ module es {
maxY = pt.y;
}
return this.fromMinMaxVector(new Vector2(minX, minY), new Vector2(maxX, maxY));
return this.fromMinMaxVector(new Vector2(Math.trunc(minX), Math.trunc(minY)), new Vector2(Math.trunc(maxX), Math.trunc(maxY)));
}
/**
@@ -336,10 +336,10 @@ module es {
* @param scale
*/
public static scale(rect: Rectangle, scale: Vector2) {
rect.x = rect.x * scale.x;
rect.y = rect.y * scale.y;
rect.width = rect.width * scale.x;
rect.height = rect.height * scale.y;
rect.x = Math.trunc(rect.x * scale.x);
rect.y = Math.trunc(rect.y * scale.y);
rect.width = Math.trunc(rect.width * scale.x);
rect.height = Math.trunc(rect.height * scale.y);
}
public static translate(rect: Rectangle, vec: Vector2) {