我怎样才能垂直画线 [英] how can i draw perpendicular to a line

查看:85
本文介绍了我怎样才能垂直画线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我纠正

我有一条线p0,p1及其中心点cp我想通过cp绘制一条垂直线,其长度为8

< br $>




please help me to correct
I have a line p0,p1 and its center point cp i want to draw a perpendicular through cp and its length is 8



double ANG = GetAngleBetweenPoints(p0, p1);
g.DrawLine(currentPen, p0, p1);

                                cp = GetCenterPoint(p0, p1);
                                linangl = 180 - ANG;
                               


                                xa = Convert.ToInt32(cp.X + 8 * (Math.Cos(linangl)));
                                ya = Convert.ToInt32(cp.Y + 8 * (Math.Sin(linangl)));
                             
                               g.DrawLine(redPen, cp.X, cp.Y, xa, ya);

推荐答案

这是一个基本的初等代数解决方案。它依赖于你在任何基本线性代数类中使用的方程式。



可能存在一些负面问题,等等你必须解决,而且它不适用于水平线(因为斜率是0和0的负倒数无效)。这段代码只是为了证明这背后的理论。您可能还会遇到可以轻松解决的数据类型问题。



Here''s a basic elementary algebra solution. It relies in the equations you''d use in any basic linear algebra class.

There may be some issues with negatives, etc. you''ll have to work out, and it doesn''t work with horizontal lines (because the slope is 0 and the negative reciprocal of 0 is invalid). This code is just meant to demonstrate the theory behind this. You may also run into issues with the data types which can easily be resolved.

Graphics surface = e.Graphics;

// Ready our variables and pick arbitrary points for the starter points.
Point p1 = new Point(30, 30);
Point p2 = new Point(50, 40);
Point midpoint = new Point();
Point p3 = new Point();
Point p4 = new Point();

double slope;

// Draw our original line.
surface.DrawLine(new Pen(Brushes.Black), p1, p2);

// Calculate the slope of the original line. (y2 - y1) / (x2 - x1) = slope
slope = ((double)(p2.Y - p1.Y) / (double)(p2.X - p1.X));

// Perpendicular lines have a slope of (-1 / originalSlope) -- the negative reciprocal.
slope = -1 / slope;

// Formula to find the midpoint.
midpoint.X = (p1.X + p2.X) / 2;
midpoint.Y = (p1.Y + p2.Y) / 2;

p3.X = midpoint.X;
p3.Y = midpoint.Y;

// Find the y-intercept of this equation. y=mx + b
double b = -slope * midpoint.X + midpoint.Y;

// Finally start calculating the final point.
// Add the length of the line to X.
p4.X = midpoint.X + 8;

// Now plug our slope, intercept, and new X into y=mx + b
p4.Y = (int)(slope * (midpoint.X + 8) + b);

// Draw that line!
surface.DrawLine(new Pen(Brushes.Blue), p3, p4);


//
PcbGraph is picturebox control



/ /


//

PcbGraphPaint is picturebox paint event







private void PcbGraphPaint(object sender, PaintEventArgs e)
        {

            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(0, (pcbGraph.Height/2F)), new PointF(pcbGraph.Width, (pcbGraph.Height/2F)));
            var centerPoint = new PointF((pcbGraph.Width/2F), (pcbGraph.Height/2F));
            PointF p1;
            PointF p2;
            var value = FindCircleCircleIntersections(0, (pcbGraph.Height/2F), (pcbGraph.Width), pcbGraph.Width, (pcbGraph.Height/2F), (pcbGraph.Width), out p1, out p2);
            if (value != 2) return;
            e.Graphics.DrawLine(new Pen(Color.Black), centerPoint, p1);
            e.Graphics.DrawLine(new Pen(Color.Black), centerPoint, p2);

        }

        private int FindCircleCircleIntersections(float cx0, float cy0, float radius0, float cx1, float cy1, float radius1, out PointF intersection1, out PointF intersection2)
        {
            // Find the distance between the centers.  
            float dx = cx0 - cx1;
            float dy = cy0 - cy1;
            double dist = Math.Sqrt(dx*dx + dy*dy);
            // See how manhym solutions there are.  
            if (dist > radius0 + radius1)
            {
                // No solutions, the circles are too far apart.       
                intersection1 = new PointF(float.NaN, float.NaN);
                intersection2 = new PointF(float.NaN, float.NaN);
                return 0;
            }
            if (dist < Math.Abs(radius0 - radius1))
            {
                // No solutions, one circle contains the other.     
                intersection1 = new PointF(float.NaN, float.NaN);
                intersection2 = new PointF(float.NaN, float.NaN);
                return 0;
            }
            if ((dist == 0) && (radius0 == radius1))
            {
                // No solutions, the circles coincide.    
                intersection1 = new PointF(float.NaN, float.NaN);
                intersection2 = new PointF(float.NaN, float.NaN);
                return 0;
            }
            // Find a and h.    
            double a = (radius0*radius0 - radius1*radius1 + dist*dist)/(2*dist);
            double h = Math.Sqrt(radius0*radius0 - a*a);
            // Find P2.      
            double cx2 = cx0 + a*(cx1 - cx0)/dist;
            double cy2 = cy0 + a*(cy1 - cy0)/dist;
            // Get the points P3.      
            intersection1 = new PointF((float) (cx2 + h*(cy1 - cy0)/dist), (float) (cy2 - h*(cx1 - cx0)/dist));
            intersection2 = new PointF((float) (cx2 - h*(cy1 - cy0)/dist), (float) (cy2 + h*(cx1 - cx0)/dist));
            // See if we have 1 or 2 solutions.     
            if (dist == radius0 + radius1) return 1;
            return 2;
        }


这篇关于我怎样才能垂直画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆