1
我想在另一個矩形內的矩形上應用一個變換....並且有相當多的困難。這裏是什麼,我想實現一個例子,旋轉將始終在90度的增量:在c中旋轉一個內部矩形(或方形)#
我已經左下方X/Y,寬度和兩個外部的高度,內部矩形....我試圖爲轉換後的內部矩形計算這些相同的值。
我的嘗試可以在下面找到,我試着圍繞大矩形的中心旋轉所有的四個角,然後把它們放在一起作爲矩形。這可能無法正常工作,因爲旋轉期間較大的矩形寬度/高度會發生變化。有誰知道一個公式來完成這個?如果有人能指點我一些很棒的資源。
我的代碼:
Vector2 center = new Vector2(largeRectWidth/2.0f, largeRectHeight/2.0f);
Rect innerRectRotated = RotateRectangleAroundPivot(innerRect, center, this.Rotation);
public static Rect RotateRectangleAroundPivot(Rect rect,
Vector2 pivot,
float rotation)
{
Vector2 leftTop = new Vector2(rect.x, rect.y + rect.height);
Vector2 rightTop = new Vector2(rect.x + rect.width, rect.y + rect.height);
Vector2 leftBottom = new Vector2(rect.x, rect.y);
Vector2 rightBottom = new Vector2(rect.x + rect.width, rect.y);
leftTop = RotatePointAroundPivot(leftTop, pivot, rotation);
rightTop = RotatePointAroundPivot(rightTop, pivot, rotation);
leftBottom = RotatePointAroundPivot(leftBottom, pivot, rotation);
rightBottom = RotatePointAroundPivot(rightBottom, pivot, rotation);
Vector2 min = Vector2.Min(Vector2.Min(leftTop, rightTop),
Vector2.Min(leftBottom, rightBottom));
Vector2 max = Vector2.Max(Vector2.Max(leftTop, rightTop),
Vector2.Max(leftBottom, rightBottom));
return new Rect(min.x, min.y, (max.x - min.x), (max.y - min.y));
}
public static Vector2 RotatePointAroundPivot(Vector2 point, Vector2 pivot, float angle)
{
angle = angle * Mathf.PI/180.0f;
return new Vector2((float)(Math.Cos(angle) * (point.x - pivot.x) - Math.Sin(angle) * (point.y - pivot.y) + pivot.x), (float)(Math.Sin(angle) * (point.x - pivot.x) + Math.Cos(angle) * (point.y - pivot.y) + pivot.y));
}