如何在知道半径和中心点的情况下计算圆上的点 [英] How to calculate a point on a circle knowing the radius and center point

查看:211
本文介绍了如何在知道半径和中心点的情况下计算圆上的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的问题,它涉及对我不确定的数学的理解.

I have a complicated problem and it involves an understanding of Maths I'm not confident with.

有些轻微的背景可能会有所帮助.我正在为儿童构建3D火车模拟器,它将在使用WebGL的浏览器中运行.我正在尝试创建一个点网络来放置轨道资产(参见图片),并为火车的行驶提供参考.

Some slight context may help. I'm building a 3D train simulator for children and it will run in the browser using WebGL. I'm trying to create a network of points to place the track assets (see image) and provide reference for the train to move along.

为了帮助解释我的问题,我创建了一个可视化表示形式,因为我是一个可以脚本编写的设计师,而实际上不是程序员或数学家:

To help explain my problem I have created a visual representation as I am a designer who can script and not really a programmer or a mathematician:

基本上,我有3种形状(图A,B和C),尽管它们具有宽度,但可以表示为A和曲线的直线(B和C).曲线B& C是从A派生(弯曲修改)而成的,所以长度都相同(112).曲线(B& C)的半径(r)为285.5,弯曲的角度(a)为. 22.5°.

Basically, I have 3 shapes (Figs. A, B & C) and although they have width, can be represented as a straight line for A and curves (B & C). Curves B & C are derived (bend modified) from A so are all the same length (l) which is 112. The curves (B & C) each have a radius (r) of 285.5 and the (a) angle they were bent at was 22.5°.

每个形状(A,B和C)都有一个配准点(起点),每个绿点附在它们的中心.

Each shape (A, B & C) has a registration point (start point) illustrated by the centre of the green boxes attached to each of them.

我要做的是创建一个从0,0(使用标准笛卡尔坐标)开始的轨迹"网络.

What I am trying to do is create a network of "track" starting at 0, 0 (using standard Cartesian coordinates).

我的问题是在曲线之后将下一个元素放置在哪里.如果它是笔直的轨道,那没有问题,因为我可以将长度用作沿y轴的恒定偏移量,但这会很无聊,因此我需要添加曲线.

My problem is where to place the next element after a curve. If it were straight track then there is no problem as I can use the length as a constant offset along the y axis but that would be boring so I need to add curves.

图. D.演示了可能的轨道布局的示例,但是请理解,我并不是在寻找静态答案(基于图像中所有内容的位置),无论如何配置轨道,我都需要一个适用的公式.

Fig. D. demonstrates an example of a possible track layout but please understand that I am not looking for a static answer (based on where everything is positioned in the image), I need a formula that can be applied no matter how I configure the track.

使用图D.我试图找出将第二个弯曲元素放置在第一个之后的位置.在给定圆心的中心坐标和半径的情况下,我使用公式绘制了圆的圆周点(图E.).

Using Fig. D. I tried to work out where to place the second curved element after the first one. I used the formula for plotting a point of the circumference of a circle given its centre coordinates and radius (Fig. E.).

我有一点1,因为那只是设置直线长度(y位置)的一种情况.我可以很容易地算出圆心,因为这只是偏移y位置,半径(r)(x位置)和角度(a)的偏移始终为22.5°(顺便说一下,已转换为弧度)按照公式要求).

I had point 1 as that was simply a case of setting the length (y position) of the straight line. I could easily work out the centre of the circle because that's just the offset y position, the offset of the radius (r) (x position) and the angle (a) which is always 22.5° (which, incidentally, was converted to Radians as per formula requirements).

在将值传递给公式后,我没有获得正确的结果,因为公式假定我从3点开始逆时针工作,因此我必须从(a)中减去180并将其转换为Radians,得到预期的结果.

After passing the values through the formula I didn't get the correct result because the formula assumed I was working anti-clockwise starting at 3 o'clock so I had to deduct 180 from (a) and convert that to Radians to get the expected result.

那确实奏效了,如果我想创建一个180°的轨迹曲线,我可以使用相同的中心点,每次都简单地从角度减去22.5°.伟大的.但是我想要一个更动态的轨道布局,如图2和3所示. D& E.

That did work and if I wanted to create a 180° track curve I could use the same centre point and simply deducted 22.5° from the angle each time. Great. But I want a more dynamic track layout like in Figs. D & E.

那么,我将如何处理图E中的工作点5,因为它表示该曲线段的中心点?我根本不知道.

So, how would I go about working point 5 in Fig. E. because that represents the centre point for that curve segment? I simply have no idea.

还有一个额外的问题,这是这样做的正确方法,还是我使事情变得过于复杂?

Also, as a bonus question, is this the correct way to be doing this or am I over-complicating things?

这个问题是阻止我开发自己的游戏的唯一问题,而且您可以欣赏到,这有点大了,所以我感谢任何人预先做出的贡献.

This problem is the only issue stopping me from building my game and, as you can appreciate, it is a bit of a biggie so I thank anyone for their contribution in advance.

推荐答案

在构建轨道时,要放置的下一条轨道的位置必须相对于轨道当前末端的位置和方向.

As you build up the track, the position of the next piece of track to be placed needs to be relative to location and direction of the current end of the track.

我将存储一个(xy)位置和一个角度a以指示当前点(xy从0开始,a从pi/2开始)弧度,对应于从3点钟开始逆时针"系统中的直线度.

I would store an (x,y) position and an angle a to indicate the current point (with x,y starting at 0, and a starting at pi/2 radians, which corresponds to straight up in the "anticlockwise from 3-o'clock" system).

然后构造

fx = cos(a);
fy = sin(a);
lx = -sin(a);
ly = cos(a);

对应于"forward"和"left"向量相对于我们当前所面对的方向的x和y分量.如果我们想将位置向前移动一个单位,则可以使(x,y)增加(fx,fy).

which correspond to the x and y components of 'forward' and 'left' vectors relative to the direction we are currently facing. If we wanted to move our position one unit forward, we would increment (x,y) by (fx, fy).

在您的情况下,放置直线路段的规则是:

In your case, the rule for placing a straight section of track is then:

x=x+112*fx
y=y+112*fy

放置曲线的规则稍微复杂一些.对于右转弯的曲线,我们需要向前移动112 * sin(22.5°),然后向右移动112 *(1-cos(22.5°),然后顺时针旋转22.5°.在代码中,

The rule for placing a curve is slightly more complex. For a curve turning right, we need to move forward 112*sin(22.5°), then side-step right 112*(1-cos(22.5°), then turn clockwise by 22.5°. In code,

x=x+285.206*sin(22.5*pi/180)*fx // Move forward
y=y+285.206*sin(22.5*pi/180)*fy

x=x+285.206*(1-cos(22.5*pi/180))*(-lx) // Side-step right
y=y+285.206*(1-cos(22.5*pi/180))*(-ly)

a=a-22.5*pi/180 // Turn to face new direction

向左转就像向右转一样,但是角度为负.

Turning left is just like turning right, but with a negative angle.

要放置后续零件,只需再次运行此过程,使用现在更新的a值计算fxfylxly,然后递增xy取决于下一个履带的类型.

To place the subsequent pieces, just run this procedure again, calculating fx,fy, lx and ly with the now-updated value of a, and then incrementing x and y depending on what type of track piece is next.

您可能还要考虑另外一点;以我的经验,如果您坚持进行90°转弯或对称布局,通常可以使用这种零件来形成闭合回路的轨道.但是,创建不完全合并的音轨非常容易,并且看不出应如何修改它们以允许它们合并也并不明显.如果您的程序允许孩子们设计自己的布局,也许要记住一些事情.

There is one other point that you might consider; in my experience, building tracks which form closed loops with these sort of pieces usually works if you stick to making 90° turns or rather symmetric layouts. However, it's quite easy to make tracks which don't quite join up, and it's not obvious to see how they should be modified to allow them to join. Something to bear in mind perhaps if your program allows children to design their own layouts.

这篇关于如何在知道半径和中心点的情况下计算圆上的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆