X
x3000
Unregistered / Unconfirmed
GUEST, unregistred user!
正在做一个矢量图制作软件,在矩形的算法上有点问题,请大家指教
设定矩形有四个顶点,移动一个顶点时,对角顶点不动,其它两个顶点移动,使之保持矩形形状。当矩形旋转后,我的算法出现一些问题:
0 ----- 3
| |
1 ----- 2
如上图矩形有0,1,2,3四个点,当移动1,3时,使用我的算法 0和2的位置有可能互换
程序如下:
PointsList[aIndex mod 4]:= P;
PointsList[(aIndex+3) Mod 4 ] := LineCross(P,PointsList[(aIndex+2) Mod 4 ],PointsList[(aIndex+2) Mod 4 ],PointsList[(aIndex+3) Mod 4 ]);
PointsList[(aIndex+1) Mod 4 ] := LineCross(P,PointsList[(aIndex+2) Mod 4 ],PointsList[(aIndex+2) Mod 4 ],PointsList[(aIndex+1) Mod 4 ]);
function LineCross(const aModifiedPoint, AnotherPoint,KAnglePoint1, KanglePoint2: TCad2dPoint): TCad2dPoint;
var
k:TRealType;
begin
if Abs(KAnglePoint1.x - KAnglePoint2.x)<littleNum then begin
Result.x := aModifiedPoint.x;
Result.y := AnotherPoint.y;
end else if Abs(KAnglePoint1.Y - KAnglePoint2.Y)<littleNum then begin
Result.Y := aModifiedPoint.Y;
Result.X := AnotherPoint.X;
end else begin
k:=(KanglePoint1.y - KanglePoint2.y) / (KanglePoint1.X - KanglePoint2.X);
Result.x :=(k*( k* aModifiedPoint.x + AnotherPoint.y - aModifiedPoint.y) + AnotherPoint.x ) / ( K*K +1);
Result.Y :=(k*( k* AnotherPoint.y - aModifiedPoint.x + AnotherPoint.x ) + aModifiedPoint.y ) / ( K*K +1);
end;
end;
其中:
PointsList放四个点的坐标,aIndex是移动的顶点, p 是新的位置
请大家帮忙看看出了什么问题或者给一个新的算法,谢谢
设定矩形有四个顶点,移动一个顶点时,对角顶点不动,其它两个顶点移动,使之保持矩形形状。当矩形旋转后,我的算法出现一些问题:
0 ----- 3
| |
1 ----- 2
如上图矩形有0,1,2,3四个点,当移动1,3时,使用我的算法 0和2的位置有可能互换
程序如下:
PointsList[aIndex mod 4]:= P;
PointsList[(aIndex+3) Mod 4 ] := LineCross(P,PointsList[(aIndex+2) Mod 4 ],PointsList[(aIndex+2) Mod 4 ],PointsList[(aIndex+3) Mod 4 ]);
PointsList[(aIndex+1) Mod 4 ] := LineCross(P,PointsList[(aIndex+2) Mod 4 ],PointsList[(aIndex+2) Mod 4 ],PointsList[(aIndex+1) Mod 4 ]);
function LineCross(const aModifiedPoint, AnotherPoint,KAnglePoint1, KanglePoint2: TCad2dPoint): TCad2dPoint;
var
k:TRealType;
begin
if Abs(KAnglePoint1.x - KAnglePoint2.x)<littleNum then begin
Result.x := aModifiedPoint.x;
Result.y := AnotherPoint.y;
end else if Abs(KAnglePoint1.Y - KAnglePoint2.Y)<littleNum then begin
Result.Y := aModifiedPoint.Y;
Result.X := AnotherPoint.X;
end else begin
k:=(KanglePoint1.y - KanglePoint2.y) / (KanglePoint1.X - KanglePoint2.X);
Result.x :=(k*( k* aModifiedPoint.x + AnotherPoint.y - aModifiedPoint.y) + AnotherPoint.x ) / ( K*K +1);
Result.Y :=(k*( k* AnotherPoint.y - aModifiedPoint.x + AnotherPoint.x ) + aModifiedPoint.y ) / ( K*K +1);
end;
end;
其中:
PointsList放四个点的坐标,aIndex是移动的顶点, p 是新的位置
请大家帮忙看看出了什么问题或者给一个新的算法,谢谢