正交相机下 画线判断目标坐标是否在画线区域

本人小白,第一次发帖,如果做的不好的地方,请多多指教,多多包涵!
首先这个功能是项目上的,组长让我写的一个小功能,不知道对其他人有没用,我就写下来吧,后续还有一些其他功能,有时间我再写一点。
我毕竟也是小白,可能有些东西比较鸡肋,代码写的也不好。。。不多说了,开始~~~


需求是用GL画线段,然后给出一个垂直距离,判断这个线段垂直距离区域里有多少个坐标点在里面。
我这里用简单的cube代替。
几个关键点我简单说下吧

1.鼠标画两点,屏幕转世界坐标。再根据垂直距离,算出矩形框的4个点。

这个算4个点,肯定有其他好的方法,大家如果有别的思路的话。。。

public Vector3[] CaulFourWorldPo(Vector3 inStart, Vector3 inEnd)
  {
      Vector3 []FourPointPos = new Vector3[4];
      float angle = Vector2.Angle(Vector2.right, Vector3ToVector2(inEnd) - Vector3ToVector2(inStart));
      if ((inStart.x > inEnd.x && inStart.z > inEnd.z) || (inStart.x < inEnd.x && inStart.z < inEnd.z))
      {
          FourPointPos[0] = new Vector3(inStart.x - Mathf.Abs(Mathf.Sin(Mathf.PI * angle / 180f)) * distance, inStart.y, inStart.z + Mathf.Abs(Mathf.Cos(Mathf.PI * angle / 180f)) * distance);
          FourPointPos[1] = new Vector3(inStart.x + Mathf.Abs(Mathf.Sin(Mathf.PI * angle / 180f)) * distance, inStart.y, inStart.z - Mathf.Abs(Mathf.Cos(Mathf.PI * angle / 180f)) * distance);
          FourPointPos[2] = new Vector3(inEnd.x + Mathf.Abs(Mathf.Sin(Mathf.PI * angle / 180f)) * distance, inStart.y, inEnd.z - Mathf.Abs(Mathf.Cos(Mathf.PI * angle / 180f)) * distance);
          FourPointPos[3] = new Vector3(inEnd.x - Mathf.Abs(Mathf.Sin(Mathf.PI * angle / 180f)) * distance, inStart.y, inEnd.z + Mathf.Abs(Mathf.Cos(Mathf.PI * angle / 180f)) * distance);
          return FourPointPos;
      }
      if ((inStart.x > inEnd.x && inStart.z < inEnd.z) || (inStart.x < inEnd.x && inStart.z > inEnd.z))
      {
          FourPointPos[0] = new Vector3(inStart.x - Mathf.Abs(Mathf.Sin(Mathf.PI * angle / 180f)) * distance, inStart.y, inStart.z - Mathf.Abs(Mathf.Cos(Mathf.PI * angle / 180f)) * distance);
          FourPointPos[1] = new Vector3(inStart.x + Mathf.Abs(Mathf.Sin(Mathf.PI * angle / 180f)) * distance, inStart.y, inStart.z + Mathf.Abs(Mathf.Cos(Mathf.PI * angle / 180f)) * distance);
          FourPointPos[2] = new Vector3(inEnd.x + Mathf.Abs(Mathf.Sin(Mathf.PI * angle / 180f)) * distance, inStart.y, inEnd.z + Mathf.Abs(Mathf.Cos(Mathf.PI * angle / 180f)) * distance);
          FourPointPos[3] = new Vector3(inEnd.x - Mathf.Abs(Mathf.Sin(Mathf.PI * angle / 180f)) * distance, inStart.y, inEnd.z - Mathf.Abs(Mathf.Cos(Mathf.PI * angle / 180f)) * distance);
          return FourPointPos;
      }
      return FourPointPos;
  }


2.判断坐标点是否在区域里
首先是判断该点和线段两点的夹角是否小于90,并且到线段的垂直距离,小于最先给出的那个值。
夹角就用 Vector2.Angle判断就好了。

垂直距离用初中公式。

private float PointToStraightlineDistance(Vector3 inStart, Vector3 inEnd, Vector3 targetPoint)
    {
        Vector2 startVector2 = Vector3ToVector2(inStart);
        Vector2 endVector2 = Vector3ToVector2(inEnd);
        float A = endVector2.y - startVector2.y;
        float B = startVector2.x - endVector2.x;
        float C = endVector2.x * startVector2.y - startVector2.x * endVector2.y;
        float denominator = Mathf.Sqrt(A * A + B * B);
        Vector2 pointVe2 = Vector3ToVector2(targetPoint);
        return Mathf.Abs((A * pointVe2.x + B * pointVe2.y + C) / denominator);
    }
    private Vector2 Vector3ToVector2(Vector3 vector3)
    {
        return new Vector2(vector3.x, vector3.z);
    }


3.把上方的四个点,用于GL画线就好了。然后再判断,画线是用的下方这个。。

void OnPostRender()
        if (drawRectangle)
        {
            GL.PushMatrix()
            if (!rectMat)
                return;
            rectMat.SetPass(0);
            GL.LoadPixelMatrix()
            GL.Begin(GL.QUADS);
            GL.Color(new Color(rectColor.r, rectColor.g, rectColor.b, 0.1f));
            GL.Vertex3(targetPointDistancePos[0].x, targetPointDistancePos[0].y, 0);
            GL.Vertex3(targetPointDistancePos[1].x, targetPointDistancePos[1].y, 0);
            GL.Vertex3(targetPointDistancePos[2].x, targetPointDistancePos[2].y, 0);
            GL.Vertex3(targetPointDistancePos[3].x, targetPointDistancePos[3].y, 0);
            GL.End();
            GL.Begin(GL.LINES);
            GL.Color(rectColor)
            GL.Vertex3(targetPointDistancePos[0].x, targetPointDistancePos[0].y, 0);
            GL.Vertex3(targetPointDistancePos[1].x, targetPointDistancePos[1].y, 0);
            GL.Vertex3(targetPointDistancePos[1].x, targetPointDistancePos[1].y, 0);
            GL.Vertex3(targetPointDistancePos[2].x, targetPointDistancePos[2].y, 0);
            GL.Vertex3(targetPointDistancePos[2].x, targetPointDistancePos[2].y, 0);
            GL.Vertex3(targetPointDistancePos[3].x, targetPointDistancePos[3].y, 0);
            GL.Vertex3(targetPointDistancePos[3].x, targetPointDistancePos[3].y, 0);
            GL.Vertex3(targetPointDistancePos[0].x, targetPointDistancePos[0].y, 0);
            GL.End();
            GL.PopMatrix()
        }
    }

 

大致效果

链接:https://pan.baidu.com/s/1sNCiQC-T-b2DwTmMPxsg5A 
提取码:d0wa 
复制这段内容后打开百度网盘手机App,操作更方便哦

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值