在Unity3D中利用描点法画圆的方法上一篇博客已写出 https://blog.csdn.net/weixin_42513339/article/details/83210495
这次讲述怎么让两个圆融合。
代码如下:
int Pos_Z = 0;
public int N; //半圆采样点个数
Vector3 CicleCenter1 = new Vector3(-0.3f, 0, 0);//两个圆圆心
Vector3 CicleCenter2 = new Vector3(1f, 0, 0);
List<List<float>> RoundPoints1 = new List<List<float>>();
List<float> xPoints1 = new List<float>();
List<float> yPoints1 = new List<float>();
List<List<float>> RoundPoints2 = new List<List<float>>();
List<float> xPoints2 = new List<float>();
List<float> yPoints2 = new List<float>();
List<int> DeleteIndex1 = new List<int>();
List<int> DeleteIndex2 = new List<int>();
// Use this for initialization
void Start () {
for (int i = 0; i < N; i++)
{
float x1 = -1.3f + i * (2f / N);
float y1 = Mathf.Sqrt(1 - Mathf.Pow(x1 + 0.3f, 2));
xPoints1.Add(x1);
yPoints1.Add(y1);
}
for (int i = N; i < 2*N+1; i++)
{
float x1 = 0.7f - (i-N) * (2f / N);
float y1 = -Mathf.Sqrt(1 - Mathf.Pow(x1 + 0.3f, 2));
xPoints1.Add(x1);
yPoints1.Add(y1);
}
for (int i = 0; i < N; i++)
{
float x2 = 0 + i * (2f / N);
float y2 = Mathf.Sqrt(1 - Mathf.Pow(x2 - 1f, 2));
xPoints2.Add(x2);
yPoints2.Add(y2);
}
for (int i = N; i < 2*N+1; i++)
{
float x2 = 2 - (i-N) * (2f / N);
float y2 = -Mathf.Sqrt(1 - Mathf.Pow(x2 - 1f, 2));
xPoints2.Add(x2);
yPoints2.Add(y2);
}
RoundPoints1.Add(xPoints1);
RoundPoints1.Add(yPoints1);
RoundPoints2.Add(xPoints2);
RoundPoints2.Add(yPoints2);
#if true //1
//一般判断距离两个圆心max大小
for (int i = 0; i < 2 * N + 1; i++)//xPoints1.Count - 1
{
float MyDistance1 = Vector3.Distance(CicleCenter1, new Vector3(xPoints1[i], yPoints1[i], 0));
float MyDistance2 = Vector3.Distance(CicleCenter2, new Vector3(xPoints1[i], yPoints1[i], 0));
if (MyDistance1 > MyDistance2)
{
DeleteIndex1.Add(i);
}
}
for (int i = 0; i < 2 * N + 1; i++)
{
float MyDistance1 = Vector3.Distance(CicleCenter1, new Vector3(xPoints2[i], yPoints2[i], 0));
float MyDistance2 = Vector3.Distance(CicleCenter2, new Vector3(xPoints2[i], yPoints2[i], 0));
if (MyDistance1 < MyDistance2)
{
DeleteIndex2.Add(i);
}
}
#endif
//删除交点;倒叙删除
for (int i = DeleteIndex1.Count - 1; i>=0 ; i--)
{
xPoints1.RemoveAt(DeleteIndex1[i]);
yPoints1.RemoveAt(DeleteIndex1[i]);
}
for (int i = DeleteIndex2.Count - 1; i >= 0; i--)
{
xPoints2.RemoveAt(DeleteIndex2[i]);
yPoints2.RemoveAt(DeleteIndex2[i]);
}
}
// Update is called once per frame
void Update () {
#if false
假设两个圆形,方程分别为 (x + 0.3)2 + y2 = 1 ; ( x - 1 )2 + y2 = 1;
#endif
for (int i = 0; i < xPoints1.Count-1; i++)
{
if (i == DeleteIndex1[0]-1)
{ }
else
{
Debug.DrawLine(new Vector3(xPoints1[i], yPoints1[i], Pos_Z),
new Vector3(xPoints1[i + 1], yPoints1[i + 1], Pos_Z),Color.red);
}
}
for (int i = 0; i < xPoints2.Count-1; i++)
{
if (i == DeleteIndex2[0] - 1)
{ }
else
{
Debug.DrawLine(new Vector3(xPoints2[i], yPoints2[i], Pos_Z),
new Vector3(xPoints2[i + 1], yPoints2[i + 1], Pos_Z), Color.red);
}
}
}
主要是判断圆上的点距离两个球心距离,然后判断距离,若距离小于原来的,便删除这个点
实现效果如下:
点合并版本
这里我把融合的形状的点变成一个数组,这样容易编辑和开发。
代码如下:(跟上面的相比,只是加了融合点的代码,修改了Update函数)
int Pos_Z = 0;
public int N; //半圆采样点个数
Vector3 CicleCenter1 = new Vector3(-0.3f, 0, 0);
Vector3 CicleCenter2 = new Vector3(1f, 0, 0);
List<List<float>> RoundPoints1 = new List<List<float>>();
List<float> xPoints1 = new List<float>();
List<float> yPoints1 = new List<float>();
List<List<float>> RoundPoints2 = new List<List<float>>();
List<float> xPoints2 = new List<float>();
List<float> yPoints2 = new List<float>();
List<int> DeleteIndex1 = new List<int>();
List<int> DeleteIndex2 = new List<int>();
//new points
List<float> xNewPoints = new List<float>();
List<float> yNewPoints = new List<float>();
// Use this for initialization
void Start () {
for (int i = 0; i < N; i++)
{
float x1 = -1.3f + i * (2f / N);
float y1 = Mathf.Sqrt(1 - Mathf.Pow(x1 + 0.3f, 2));
xPoints1.Add(x1);
yPoints1.Add(y1);
}
for (int i = N; i < 2*N+1; i++)
{
float x1 = 0.7f - (i-N) * (2f / N);
float y1 = -Mathf.Sqrt(1 - Mathf.Pow(x1 + 0.3f, 2));
xPoints1.Add(x1);
yPoints1.Add(y1);
}
for (int i = 0; i < N; i++)
{
float x2 = 0 + i * (2f / N);
float y2 = Mathf.Sqrt(1 - Mathf.Pow(x2 - 1f, 2));
xPoints2.Add(x2);
yPoints2.Add(y2);
}
for (int i = N; i < 2*N+1; i++)
{
float x2 = 2 - (i-N) * (2f / N);
float y2 = -Mathf.Sqrt(1 - Mathf.Pow(x2 - 1f, 2));
xPoints2.Add(x2);
yPoints2.Add(y2);
}
RoundPoints1.Add(xPoints1);
RoundPoints1.Add(yPoints1);
RoundPoints2.Add(xPoints2);
RoundPoints2.Add(yPoints2);
#if true //1
//一般判断max大小
for (int i = 0; i < 2 * N + 1; i++)//xPoints1.Count - 1
{
float MyDistance1 = Vector3.Distance(CicleCenter1, new Vector3(xPoints1[i], yPoints1[i], 0));
float MyDistance2 = Vector3.Distance(CicleCenter2, new Vector3(xPoints1[i], yPoints1[i], 0));
if (MyDistance1 > MyDistance2)
{
DeleteIndex1.Add(i);
}
}
for (int i = 0; i < 2 * N + 1; i++)
{
float MyDistance1 = Vector3.Distance(CicleCenter1, new Vector3(xPoints2[i], yPoints2[i], 0));
float MyDistance2 = Vector3.Distance(CicleCenter2, new Vector3(xPoints2[i], yPoints2[i], 0));
if (MyDistance1 < MyDistance2)
{
DeleteIndex2.Add(i);
}
}
#endif
//删除交点;倒叙删除
for (int i = DeleteIndex1.Count - 1; i>=0 ; i--)
{
xPoints1.RemoveAt(DeleteIndex1[i]);
yPoints1.RemoveAt(DeleteIndex1[i]);
}
for (int i = DeleteIndex2.Count - 1; i >= 0; i--)
{
xPoints2.RemoveAt(DeleteIndex2[i]);
yPoints2.RemoveAt(DeleteIndex2[i]);
}
#if true //点融合代码!!!!
for (int i = 0; i < DeleteIndex1[0]; i++)
{
xNewPoints.Add(xPoints1[i]);
yNewPoints.Add(yPoints1[i]);
}
for (int i = 0; i < xPoints2.Count; i++)
{
xNewPoints.Add(xPoints2[i]);
yNewPoints.Add(yPoints2[i]);
}
for (int i = DeleteIndex1[0]; i < xPoints1.Count; i++)
{
xNewPoints.Add(xPoints1[i]);
yNewPoints.Add(yPoints1[i]);
}
#endif
}
// Update is called once per frame
void Update () {
#if false
假设两个圆形,方程分别为 (x + 0.3)2 + y2 = 1 ; ( x - 1 )2 + y2 = 1;
#endif
for (int i = 0; i < xNewPoints.Count - 1; i++)
{
Debug.DrawLine(new Vector3(xNewPoints[i], yNewPoints[i], Pos_Z),
new Vector3(xNewPoints[i + 1], yNewPoints[i + 1], Pos_Z), Color.red);
}
}