计算两条直线的交点

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {


        }
        public static float Angle(Point cen, Point first, Point second)
        {
            float dx1, dx2, dy1, dy2;
            float angle;


            dx1 = first.X - cen.X;
            dy1 = first.Y - cen.Y;


            dx2 = second.X - cen.X;


            dy2 = second.Y - cen.Y;


            float c = (float)Math.Sqrt(dx1 * dx1 + dy1 * dy1) * (float)Math.Sqrt(dx2 * dx2 + dy2 * dy2);


            if (c == 0) return -1;


            angle = (float)Math.Acos((dx1 * dx2 + dy1 * dy2) / c);




            return angle;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            //Point p1=new Point(100,100);
            //Point p2=new Point(100,200);
            //Point p3=new Point(200,200);
            //textBox7.Text = Angle(p1, p2, p3).ToString();
            //textBox8.Text = (Angle(p1, p2, p3) * 180 / Math.PI).ToString();
            //textBox7.Text = (Math.Atan2(100, 100)* 180 / Math.PI).ToString();
            DrawJT(Button1,Button2);


            Point a = new Point(Button1.Location.X+(int)(Button1.Width / 2), Button1.Location.Y + (int)(Button1.Height / 2));
            Point b = new Point(Button2.Location.X+(int)(Button2.Width / 2), Button2.Location.Y + (int)(Button2.Height / 2));
            Point c = new Point(Button1.Location.X + (int)(Button1.Width), Button1.Location.Y);
            Point d = new Point(Button1.Location.X +(int)(Button1.Width), Button1.Location.Y+ (int)(Button1.Width));
            //getPoit(Button1.l, b, c, d);
            //c = new Point(1, 1);
            //d = new Point(10, 10);
            //a = new Point(2, 12);
            //b = new Point(7, 2);
            //sa( a, b, c, d);
            PointF Z = GetIntersection(a, b, c, d);
            textBox7.Text = Z.X.ToString(); textBox8.Text = Z.Y.ToString();
        }


        public void DrawJT(Button button1, Button button2)
        {
            decimal bl_W = 1;
            decimal bl_H = 1;
            Image img = pictureBoxTS.Image;
            
            int startX, startY, endX, endY;
            startX = (int)((Button1.Location.X + (int)(Button1.Width / 2)) / bl_W);
            startY = (int)((Button1.Location.Y + (int)(Button1.Height / 2)) / bl_H);
            endX = (int)((Button2.Location.X + (int)(Button2.Width / 2)) / bl_W);
            endY = (int)((Button2.Location.Y + (int)(Button2.Height / 2)) / bl_H);


            //startX = Button1.Location.X + (int)(Button1.Width /2);


            Point ex1 = new Point(startX , Button1.Location.Y);
            Point ey1 = new Point(startX , Button1.Location.Y + Button1.Height);
            PointF pt1=GetIntersection(new Point(startX,startY),new Point(endX,endY),ex1,ey1);


            Point ex2 = new Point(endX , Button2.Location.Y);
            Point ey2 = new Point(endX , Button2.Location.Y + Button2.Height);
            PointF pt2 = GetIntersection(new Point(startX, startY), new Point(endX, endY), ex2, ey2);




            Pen p = new Pen(Color.Orange, 3);
            Graphics g = Graphics.FromImage(img);
            p.EndCap = LineCap.ArrowAnchor;
            p.DashStyle = DashStyle.Custom;


            p.CustomEndCap = new AdjustableArrowCap((float)(p.Width * 2), (float)(p.Width * 2), true);
            //g.DrawLine(p, startX, startY, endX,  endY);
            //g.DrawLine(p, pt1.X, pt1.Y, pt2.X, pt2.Y);
            
            p = new Pen(Color.RoyalBlue, 4);
            p.CustomEndCap = new AdjustableArrowCap((float)(p.Width * 2), (float)(p.Width * 2), true);


            g.DrawLine(p, pt1.X, pt1.Y, pt2.X, pt2.Y);


            g.Dispose();
            p.Dispose();
            pictureBoxTS.Refresh();
        }

        


        bool cocircle(Point P, Point P1, Point P2)
        {
            int x1 = P1.X;
            int y1 = P1.Y;
            int x2 = P2.X;
            int y2 = P2.Y;


            int x3 = P.X;
            int y3 = P.Y;


            double a, b, e;


            a = (x1 + x2) * (x1 - x2) + (y1 + y2) * (y1 - y2);
            b = (x3 + x2) * (x3 - x2) + (y3 + y2) * (y3 - y2);
            e = (x1 - x2) * (y3 - y2) - (x2 - x3) * (y2 - y1);


            if (Math.Equals(e, 0))
            {
                return true;
            }
            return false;
        }
        private bool inLine(Point P, Point P1,Point P2)
        { 
       int  x1=P1.X;
            int y1=P1.Y;
       int  x2=P2.X;
            int y2=P2.Y;


       int  x=P.X;
            int y=P.Y;


//P(x,y)点,如果在直线上,那么它必须满足( 0 <= t <= 1 )。
//P = P1 + t(P2 - P1)
            decimal t1 = (x - x1)/(x2 - x1);
            decimal t2 = (y - y1) / (y2 - y1);
            if (0 <= t1 && t1 <= 1 && 0 <= t2 && t2 <= 1)
            {
                return true;
            }
            return false;
        }


        //判断坐标点P到线段两端点(AB)的距离。
        //只要满足公式 AB=PA+PB 点P就在线段点上
        private bool isPointInLine(Point pt, Point lnstart, Point lnEnd)
        {
            int x1 = lnstart.X;
            int y1 = lnstart.Y;
            int x2 =lnEnd.X;
            int y2 =lnEnd.Y;
            int x = pt.X;
            int y = pt.Y;
            if ( x1 == x || x2 == x)
            {
                if (x1 == x2 || x == x1 && y == y1 || x == x2 || y == y2)
                {
                 return true   ;//在
                }
                else
                {
                   return false ;//不在
                }
            }
            else if ((x1 - x) * (y1 - y) == (x2 - x) * (y2 - y))
            {
               return true ;//在
            }
            else
            {
              return false  ;//不在
            }
        }






        /// <summary>
        /// 计算两条直线的交点
        /// </summary>
        /// <param name="p1">L1的点1坐标</param>
        /// <param name="p2">L1的点2坐标</param>
        /// <param name="p3">L2的点1坐标</param>
        /// <param name="p4">L2的点2坐标</param>
        /// <returns></returns>
        public  PointF GetIntersection(PointF p1, PointF p2, PointF p3, PointF p4)
        {
            /*
             * L1,L2都存在斜率的情况:
             * 直线方程L1: ( y - y1 ) / ( y2 - y1 ) = ( x - x1 ) / ( x2 - x1 ) 
             * => y = [ ( y2 - y1 ) / ( x2 - x1 ) ]( x - x1 ) + y1
             * 令 a = ( y2 - y1 ) / ( x2 - x1 )
             * 有 y = a * x - a * x1 + y1   .........1
             * 直线方程L2: ( y - y3 ) / ( y4 - y3 ) = ( x - x3 ) / ( x4 - x3 )
             * 令 b = ( y4 - y3 ) / ( x4 - x3 )
             * 有 y = b * x - b * x3 + y3 ..........2
             * 
             * 如果 a = b,则两直线平等,否则, 联解方程 1,2,得:
             * x = ( a * x1 - b * x3 - y1 + y3 ) / ( a - b )
             * y = a * x - a * x1 + y1
             * 
             * L1存在斜率, L2平行Y轴的情况:
             * x = x3
             * y = a * x3 - a * x1 + y1
             * 
             * L1 平行Y轴,L2存在斜率的情况:
             * x = x1
             * y = b * x - b * x3 + y3
             * 
             * L1与L2都平行Y轴的情况:
             * 如果 x1 = x3,那么L1与L2重合,否则平等
             * 
            */


            float a = 0, b = 0;
            int state = 0;


            if (p1.X != p2.X)
            {
                a = (p2.Y - p1.Y) / (p2.X - p1.X);
                state |= 1;
            }
            if (p3.X != p4.X)
            {
                b = (p4.Y - p3.Y) / (p4.X - p3.X);
                state |= 2;
            }
            switch (state)
            {
                case 0: //L1与L2都平行Y轴
                    {
                        if (p1.X == p3.X)
                        {
                            throw new Exception("两条直线互相重合,且平行于Y轴,无法计算交点。");
                        }
                        else
                        {
                            throw new Exception("两条直线互相平行,且平行于Y轴,无法计算交点。");
                        }
                    }
                case 1: //L1存在斜率, L2平行Y轴
                    {
                        float x = p3.X;
                        float y = a * x - a * p1.X + p1.Y;
                        return new PointF(x, y);
                    }
                case 2: //L1 平行Y轴,L2存在斜率
                    {
                        float x = p1.X;
                        float y = b * x + b * p3.X + p3.Y;
                        return new PointF(x, y);
                    }
                case 3: //L1,L2都存在斜率
                    {
                        if (a == b)
                        {
                            throw new Exception("两条直线平行或重合,无法计算交点。");
                        }
                        float x = (a * p1.X - b * p3.X - p1.Y + p3.Y) / (a - b);
                        float y = a * x - a * p1.X + p1.Y;
                        return new PointF(x, y);
                    }
            }
            throw new Exception("不可能发生的情况");
        }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值