我有三个点形成一个三角形(显然)。但我不想画这个三角形,而是在里面画一个小三角形。
如何在C中使用Cairo执行此操作?
答案 0 :(得分:3)
我认为你需要计算新三角形的顶点,它们的边与旧三角形的边相等。
查找相邻三角形边的单位向量 BD = BA / | BA |,BE = BC / | BC | ...(| BA |是向量BA的长度)
查找单位平分线 BF =(BD + BE)/ | BD + BE |
找出F点的最终位置(所需长度的平分线) BF = BF * NeededDistance /( BF x BE < / strong>)...(分母是交叉乘积,它等于Sin(ABC / 2)
对G和H顶点执行相同的操作。
Delphi代码和结果:
var
P, V: array[0..2] of TPoint;
i, inext, iprev: Integer;
nx, ny, px, py, mx, my, coeff: Double;
distance: Integer;
procedure NormalizeVector(var dx, dy: Double);
var
revlen: Double;
begin
revlen := 1.0 / Hypot(dx, dy);
dx := dx * revlen;
dy := dy * revlen;
end;
begin
Canvas.Brush.Color := Color;
Canvas.FillRect(ClientRect); //clean the blackboard
Randomize;
//big triangle vertices
for i := 0 to 2 do
P[i] := Point(Random(500), Random(500));
//draw big triangle
Canvas.Brush.Style := bsClear;
Canvas.Polygon(P);
for i := 0 to 2 do begin
inext := (i + 1) mod 3; // next vertice index
iprev := (i - 1 + 3) mod 3; // previous vertice index
nx := P[inext].X - P[i].X; //vector to the next vertice
ny := P[inext].Y - P[i].Y;
px := P[iprev].X - P[i].X; //vector to the previous vertice
py := P[iprev].Y - P[i].Y;
NormalizeVector(nx, ny); //make unit vectors
NormalizeVector(px, py);
mx := nx + px;
my := ny + py;
NormalizeVector(mx, my); //unit bisector
distance := 20;
coeff := distance / (mx * py - my * px);
mx := mx * coeff;
my := my * coeff;
//inner triangle vertice
V[i] := Point(P[i].X + Round(mx), P[i].Y + Round(my));
end;
//draw inner triangle
Canvas.Polygon(V);