【问题标题】:How to calculate which grid squares a circle touches?如何计算一个圆接触的网格正方形?
【发布时间】:2016-05-06 19:02:55
【问题描述】:

我有一张由大约 4,000,000 像素组成的图像。每个像素都有一个地理 XY 坐标(坐标位于像素的中心),每个像素对应一个 A x A 平方米。

假设我在图像上放了一个随机点(使用随机 XY 坐标)并围绕该点画一个半径为 B 米的圆:

我的问题是:我怎样才能有效地计算出圆所接触的正方形?

【问题讨论】:

标签: matlab geometry


【解决方案1】:

您需要一个有效的函数来确定圆是否与正方形相交(也包括,也位于里面)。这个(Delphi 实现)不使用三角蚂蚁平方根。

请注意,此函数适用于单个正方形。但是这种方法可能会针对方形网格进行修改-您可以评估一次整列的水平移位值,一次评估整行的垂直移位值,然后使用计算值SquaredDist = SqDistForRow[Row] + SqDistForColumn[Col]

function IsCircleIntersectsSquare
            (CX, CY, CR: Integer; {circle}
             SX, SY, A: Integer{square}): Boolean;
var
  halfA, dx, dy, t, SquaredDist: Integer;
begin

  //halfsize
  halfA := A div 2;

  //distances to square center
  dx := CX - SX;
  dy := CY - SY;

  SquaredDist := 0;

  //square sides divide plane to 9 parts
  t := dx + halfA;
  if t < 0 then
    SquaredDist := t * t
  else begin
    t := dx - halfA;
    if t > 0 then
      SquaredDist := t * t
  end;

  t := dy + halfA;
  if t < 0 then
    SquaredDist := SquaredDist + t * t
  else begin
    t := dy - halfA;
    if t > 0 then
      SquaredDist := SquaredDist + t * t
  end;

  Result := SquaredDist <= CR * CR
end;

【讨论】:

  • 谢谢!欣赏它! =)
  • 对我正在写的物理模拟非常有用!这个算法有名字吗,还是你自己想出来的?
  • @a52 我曾多次看到类似的方法,但不认为它有名字。
  • @MBo 真正独一无二!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 2021-10-12
  • 1970-01-01
  • 2018-01-17
  • 2015-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多