判断两条线是否相交的算法(C#)

   1:          /// <summary>
   2:          /// 判断两条线是否相交
   3:          /// </summary>
   4:          /// <param name="a">线段1起点坐标</param>
   5:          /// <param name="b">线段1终点坐标</param>
   6:          /// <param name="c">线段2起点坐标</param>
   7:          /// <param name="d">线段2终点坐标</param>
   8:          /// <param name="intersection">相交点坐标</param>
   9:          /// <returns>是否相交 0:两线平行  -1:不平行且未相交  1:两线相交</returns>
  10:          private int GetIntersection(Point a, Point b, Point c, Point d ,ref Point intersection)
  11:          {
  12:              //判断异常
  13:              if (Math.Abs(b.X - a.Y) + Math.Abs(b.X - a.X) + Math.Abs(d.Y - c.Y) + Math.Abs(d.X - c.X) == 0)
  14:              {
  15:                  if (c.X - a.X == 0)
  16:                  {
  17:                     Debug.Print("ABCD是同一个点!");
  18:                  }
  19:                  else
  20:                  {
  21:                      Debug.Print("AB是一个点,CD是一个点,且AC不同!");
  22:                  }
  23:                  return 0;
  24:              }
  25:             
  26:              if (Math.Abs(b.Y - a.Y) + Math.Abs(b.X - a.X) == 0)
  27:              {
  28:                  if ((a.X - d.X) * (c.Y - d.Y) - (a.Y - d.Y) * (c.X - d.X) == 0)
  29:                  {
  30:                      Debug.Print ("A、B是一个点,且在CD线段上!");
  31:                  }
  32:                  else
  33:                  {
  34:                       Debug.Print ("A、B是一个点,且不在CD线段上!");
  35:                  }
  36:                  return 0;
  37:              }
  38:              if (Math.Abs(d.Y - c.Y) + Math.Abs(d.X - c.X) == 0)
  39:              {
  40:                  if ((d.X - b.X) * (a.Y - b.Y) - (d.Y - b.Y) * (a.X - b.X) == 0)
  41:                  {
  42:                      Debug.Print ("C、D是一个点,且在AB线段上!");
  43:                  }
  44:                  else
  45:                  {
  46:                      Debug.Print ("C、D是一个点,且不在AB线段上!");
  47:                  }
  48:              }
  49:             
  50:              if ((b.Y - a.Y) * (c.X - d.X) - (b.X - a.X) * (c.Y - d.Y) == 0)
  51:              {
  52:                  Debug.Print ("线段平行,无交点!");
  53:                  return 0;
  54:              }
  55:             
  56:              intersection.X = ((b.X - a.X) * (c.X - d.X) * (c.Y - a.Y) - c.X * (b.X - a.X) * (c.Y - d.Y) + a.X * (b.Y - a.Y) * (c.X - d.X)) / ((b.Y - a.Y) * (c.X - d.X) - (b.X - a.X) * (c.Y - d.Y));
  57:              intersection.Y = ((b.Y - a.Y) * (c.Y - d.Y) * (c.X - a.X) - c.Y * (b.Y - a.Y) * (c.X - d.X) + a.Y * (b.X - a.X) * (c.Y - d.Y)) / ((b.X - a.X) * (c.Y - d.Y) - (b.Y - a.Y) * (c.X - d.X));
  58:             
  59:              if ((intersection.X - a.X) * (intersection.X - b.X) <= 0 && (intersection.X - c.X) * (intersection.X - d.X) <= 0 && (intersection.Y - a.Y) * (intersection.Y - b.Y) <= 0 && (intersection.Y - c.Y) * (intersection.Y - d.Y) <= 0)
  60:              {
  61:                  Debug.Print ("线段相交于点(" + intersection.X + "," + intersection.Y + ")!");
  62:                  return 1; //'相交
  63:              }
  64:              else
  65:              {
  66:                  Debug.Print ("线段相交于虚交点(" + intersection.X + "," + intersection.Y + ")!");
  67:                  return -1; //'相交但不在线段上
  68:              }
  69:          }
posted @ 2012-07-09 16:31  Rick Sun  阅读(7102)  评论(0编辑  收藏  举报