简体   繁体   English

C ++计算两个圆之间的距离

[英]C++ calculate the distance between two circles

Can someone explain this? 有人可以解释吗?

double distance( int x1, int y1, int x2, int y2 )
{
    //Return the distance between the two points
    return sqrt( pow( x2 - x1, 2 ) + pow( y2 - y1, 2 ) );
}
bool check_collision( Circle &A, Circle &B )
{
    //If the distance between the centers of the circles is less than the sum of their radii
    if( distance( A.x, A.y, B.x, B.y ) < ( A.r + B.r ) )
    {
        //The circles have collided
        return true;
    }

    //If not
    return false;
}

I dont get how this bit of code 我不明白这部分代码

//Return the distance between the two points
return sqrt( pow( x2 - x1, 2 ) + pow( y2 - y1, 2 ) );

returns the distance between two circles.. Code is from http://lazyfoo.net/SDL_tutorials/lesson19/index.php 返回两个圆之间的距离。代码来自http://lazyfoo.net/SDL_tutorials/lesson19/index.php

This 这个

sqrt( pow( x2 - x1, 2 ) + pow( y2 - y1, 2 ) )

returns the euclidean distance between the circle centres. 返回圆心之间的欧式距离。 As a formula this distance is simply 作为公式,该距离只是

sqrt((a1-b1)^2 + (a2-b2)^2)

where (a1,a2) and (b1,b2) are the centres of the 2 circles. 其中(a1,a2)和(b1,b2)是2个圆的中心。

It doesn't return the distance between the circles, it does what it says "Return the distance between the two points" with a plain old Cartesean distance calculation. 它不会返回圆之间的距离,而是使用普通的旧笛卡尔距离计算来执行“返回两点之间的距离”的操作。 The programmer then passes the centers of the two circles. 然后程序员通过两个圆的中心

Then the programmer subtracts off both radii to get the distance between the circles. 然后,程序员减去两个半径即可得出圆之间的距离。 Only instead of actually bothering to subtract (s)he just compares because (s)he is only interested in a collision vs. no-collision decision. 他只是比较而不是实际去减去(s),因为他只对碰撞与非碰撞决策感兴趣。

Euclidean distance between the origin and a point (x, y) is defined by: 原点和点(x,y)之间的欧式距离定义为:

d = (x2 + y2)(1/2)

So the distance between the center points p 1 = (x 1 , y 1 ) and p 2 = (x 2 , y 2 ) of the two circles is given by translating the entire system so one center point is at the origin, and the distance to the other is computed. 因此,通过平移整个系统可以得出两个圆的中心点p 1 =(x 1 ,y 1 )和p 2 =(x 2 ,y 2 )之间的距离,因此一个中心点位于原点,计算到另一个的距离。 We do this by: 我们这样做是:

d = |p2 - p1|2
  = ((x2-x1)2 + (y2-y1)2)(1/2)

in C++ this would typically look like 在C ++中,通常看起来像

return sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) );

In this case, however, it looks like the author is making use of a pow(b, e) function which takes the first argument, b , and raises it to the power of the the second argument, e to get b e . 但是,在这种情况下,作者似乎正在使用pow(b, e)函数,该函数采用第一个参数b ,并将其提升为第二个参数e的幂,以得到b e

It returns the distance between the centers of the circles. 它返回圆心之间的距离。 Assumed Circle.x , Circle.y represents the center. 假设Circle.xCircle.y代表中心。 This is used in the rest of the computation to check for collision. 在其余的计算中使用它来检查冲突。

The distance of two points can be inferred by using Pithagroas' theorem about right-angled triangles. 可以通过使用关于直角三角形的Pithagroas定理来推断两点的距离。 In Descartes's coordinate system, if you define two points: P1(x1, y1) and P2(x2, y2), then you'll have a right-angled triangle between them: 在笛卡尔的坐标系中,如果定义两个点:P1(x1,y1)和P2(x2,y2),则它们之间将有一个直角三角形:

^ y
|
|
|
|
+ ---------x P1(x1, y1) = (12, 8)
|          |
|          | 
|          | 
+----------x-I-------x P2(x2, y2) = (24, 4)
|          |         |
|          |         |
|          |         |
+------------------------------------------------> x

Now the P1, P2, I triangle has a right angle at its point I . 现在, P1, P2, I三角形在其点I处具有直角。 So Pithagoras' theorem applies as: 因此,毕达哥拉斯定理适用于:

c ^ 2 = a ^ 2 + b ^ 2
distance ^ 2 = (x1 - x2) ^ + (y2 - y1) ^ 2
since n ^ 2 equals (-n) ^ 2 we can finally write:
distance = sqrt((x1 - x2) ^ 2 + (y1 - y2) ^ 2)

That's why. 这就是为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否有C ++函数来计算两个索引之间的距离? - Is there a C++ function to calculate distance between two indexes? 如何通过opencv计算图像中两个圆的距离 - How to calculate the distance of two circles in a image by opencv 使用指针和结构计算两点之间的距离C ++,分段错误问题 - Calculate distance between two points using pointers and structures C++, Segmentation Fault issue OpenCV,C ++:两点之间的距离 - OpenCV, C++: Distance between two points C ++:BFS计算树中每对节点之间的距离 - C++ : BFS to calculate distance between every pair of nodes in a tree 计算点和平面之间的3d距离,C ++ - Calculate the 3d distance between point and plane, C++ C ++计算输入点和保存点的结构与原点之间的距离,并根据距离打印出列表 - C++ calculate distance between structure of inputed and saved points and the origin, print out list according on distance C ++在2D数组中找到两点之间的距离 - C++ finding distance between two points in 2D array 使用 opencv/c++ 将像素转换为 mm --&gt; 两点之间的距离 - Convert pixels to mm with opencv/c++ --> Distance between two points C ++创建一个函数以获取两点之间的距离 - C++ Creating a function to get distance between two points
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM