# 绘制垂线 c#

0

Bruce Wan | 初学一级 | 园豆：0

0

` 1 void Main() 2 { 3     //假设的两个点 4      Vector2 startPoint = new Vector2(1, 60); 5     Vector2 endPoint = new Vector2(1, -60); 6      7     //中点坐标 8      Vector2 centerPoint=new Vector2((endPoint.X+startPoint.X)/2,(endPoint.Y+startPoint.Y)/2); 9     10     //CreateRotation参数是弧度11      Matrix m= Matrix.CreateRotation(3.14159265479f/2);12     //各按他们的中点旋转90度13      Vector2 ntStartpoint= Vector2.Transform(new Vector2(startPoint.X-centerPoint.X,startPoint.Y-centerPoint.Y), m);14     Vector2 ntEndpoint= Vector2.Transform(new Vector2(endPoint.X-centerPoint.X,endPoint.Y-centerPoint.Y), m);15 16     //要求的两个点17      Vector2 newStartPoint=new Vector2(ntStartpoint.X+centerPoint.X,ntStartpoint.Y+centerPoint.Y);18     Vector2 newEndPoint=new Vector2(ntEndpoint.X+centerPoint.X,ntEndpoint.Y+centerPoint.Y);19 }20 21  public struct Matrix22 {23     public float M11;24     public float M12;25     public float M13;26     public float M14;27     public float M21;28     public float M22;29     public float M23;30     public float M24;31     public float M31;32     public float M32;33     public float M33;34     public float M34;35     public float M41;36     public float M42;37     public float M43;38     public float M44;39 40     public static Matrix CreateRotation(float radians)41     {42         Matrix matrix;43         float num2 = (float)Math.Cos((double)radians);44         float num = (float)Math.Sin((double)radians);45         matrix.M11 = num2;46         matrix.M12 = num;47         matrix.M13 = 0f;48         matrix.M14 = 0f;49         matrix.M21 = -num;50         matrix.M22 = num2;51         matrix.M23 = 0f;52         matrix.M24 = 0f;53         matrix.M31 = 0f;54         matrix.M32 = 0f;55         matrix.M33 = 1f;56         matrix.M34 = 0f;57         matrix.M41 = 0f;58         matrix.M42 = 0f;59         matrix.M43 = 0f;60         matrix.M44 = 1f;61         return matrix;62     }63 }64 65  public struct Vector266 {67     public float X;68     public float Y;69 70     public Vector2(float x, float y)71     {72         this.X = x;73         this.Y = y;74     }75 76     public static Vector2 Transform(Vector2 position, Matrix matrix)77     {78         Vector2 vector;79         float num2 = ((position.X * matrix.M11) + (position.Y * matrix.M21)) + matrix.M41;80         float num = ((position.X * matrix.M12) + (position.Y * matrix.M22)) + matrix.M42;81         vector.X = num2;82         vector.Y = num;83         return vector;84     }85 }`

@Bruce Wan:谢谢你的方法，不过我按另外一种方法算出来了，下面是我的代码： Point pc=new Point (); pc.X = Convert.ToInt32(textBox9.Text); pc.Y = Convert.ToInt32(textBox10.Text); Graphics g = CreateGraphics(); Pen p = new Pen (Color.Red); Point p1 = new Point(Convert.ToInt32 ( textBox1.Text),Convert.ToInt32 ( textBox2 .Text) ); Point p2 = new Point(Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text)); g.DrawLine(p, p1, p2);//绘制已知的直线 //First calulate the slope // float slope = (p2.Y - p1.Y) / (p2.X - p1.X); float slope1 = 0; if (p2.Y == p1.Y )//平行于坐标轴 { //获取直线上任意一点，这里取其中心； g.DrawLine(p, pc.X, pc.Y + 10, pc.X, pc.Y - 10); textBox5.Text = ((p2.X + p1.X) / 2).ToString(); textBox6.Text = p1.Y.ToString(); ; textBox7.Text = ((p2.X + p1.X) / 2).ToString(); textBox8.Text = (p1.Y + 100).ToString(); } else if (p1.X == p2.X) { g.DrawLine(p, pc.X + 10, pc.Y, pc.X - 10, pc.Y); } else { float slope = (p2.Y - p1.Y) / (p2.X - p1.X); slope1 = -1 / slope;//其垂直直线的斜率 float lm = 0; lm = p1.Y - p1.X * slope;//原始直线方程;y=slope*x+lm //获取直线上任意一点，这里取其中心； float p3y = slope * (p2.X + p1.X) / 2 + lm;//原始直线与垂直直线相交与原始直线的中点，故求出对应的p3y坐标 float lm1 = p3y - ((p2.X + p1.X) / 2) * slope1;//垂直直线方程;y=slope1*x+lm1 float p4y = slope1 * p2.X + lm1;//垂直方程在横坐标为p2.x，纵坐标为p4y; textBox5.Text = ((p2.X + p1.X) / 2).ToString(); textBox6.Text = p3y.ToString(); ; textBox7.Text = p2.Y.ToString(); textBox8.Text = p4y.ToString(); g.DrawLine(p, (p2.X + p1.X) / 2, p3y, p2.X, p4y);//绘制和xy轴不平行的直线的垂线 } p.Dispose(); g.Dispose();

……，这个看起头好晕。 结贴吧

您需要登录以后才能回答，未注册用户请先注册