我有三个点形成一个三角形(显然)。但我不想画这个三角形,而是画一个较小的三角形。
我如何在 C 中使用 Cairo 来做到这一点?
我认为您需要计算边与旧三角形边等距的新三角形的顶点。
求相邻三角形边BD = BA / |BA|, BE = BC / |BC|的单位向量 ... (|BA| 是向量 BA 的长度)
求单位平分线BF = (BD + BE) / |BD + BE|
找到 F 点的最终位置(所需长度的平分线)BF = BF * NeededDistance / ( BF x BE ) ...(分母是叉积,它等于 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);