几何找到从正方形的边缘到该正方形内最大可能圆的边缘的距离

时间:2020-02-29 15:03:14

标签: geometry

数学从来不是我的强项。 :)给定一个以像素为单位的任意大小的方形画布,我在画布内部绘制了宽度为1的最大可能圆。然后,我想绘制一个以圆为中心的矩形(不一定是正方形),且角接触圆。因此,我需要一个公式,根据给定圆的直径,为该内部矩形计算左上像素的X和Y坐标。或者,给定沿画布顶部的X个像素和沿画布左侧的Y个像素,我需要一个公式来查找该圆的边缘上垂直和水平匹配点的X / Y坐标。谢谢! :)

1 个答案:

答案 0 :(得分:-1)

鉴于正弦规则适用,圆内正方形的边长应为Len =(Dia / sin(90))/ 2,然后将左上角的X和Y坐标设为两者都是((Dia-Len)div 2)。对于矩形,将X和Y均等地递增/递减。在Delphi中:

    procedure SquareInCircle(Dia: Integer; var P: TPoint; Padding: Integer = 0);
    //Given a circle of diameter Dia, find the X and Y of the
    //upper-left corner of a square with corners on the rim
    //of the circle.  Consider the diameter @ 45°, which forms
    //a right triangle from the square; the length of any side
    //of the square is the diameter / sin(90) / 2, and from that,
    //the X and Y of the upper-left are (dia - len) / 2.  Since
    //we're working in pixels, results are kept as integers.
    var
      L: Integer;
    begin
      L := Round((Dia / Sin(90)) / 2);
      P.X := ((Dia - L) div 2) + Padding;
      P.Y := P.X;
    end;

    procedure RectInCircle(Dia, Ofs: Integer; var P: TPoint; Padding: Integer = 0);
    //Ofs is the number of pixels to offset from square to rectangle.
    //If ofs is negative, the rectangle is taller than it is high;
    //if ofs is positive, the rectangle is wider than it is tall.
    //Ofs must be >= 0 and < (dia / 2).
    begin
      SquareInCircle(Dia, P, Padding);
      Dec(P.X, Ofs);
      Inc(P.Y, Ofs);
    end;
相关问题