zoukankan      html  css  js  c++  java
  • winform 根据两点求出线上所有点及画出这条线

    找出所有点:

    根据斜率按照一个方向递增,求出对应的另一个方向的整数值。

    Point pStart = new Point(0, 2);
                Point pEnd = new Point(8, 2);
                //定义线上的点
                List<Point> linePoint = new List<Point>();
                //定义x坐标的大小
                Point pointMaxX = new Point();
                Point pointMinX = new Point();
                //给x坐标大的点和小的点赋值
                if (Math.Max(pStart.X, pEnd.X) == pStart.X)
                {
                    pointMaxX = pStart;
                    pointMinX = pEnd;
                }
                else
                {
                    pointMaxX = pEnd;
                    pointMinX = pStart;
                }
                // 循环x坐标(循环满足x坐标为整数的y坐标)或者y坐标均可
                for (int i = pointMinX.X + 1; i < pointMaxX.X; i++)
                {
                    // 计算斜率
                    double k = ((double)(pointMinX.Y - pointMaxX.Y)) / (pointMinX.X - pointMaxX.X);
                    // 根据斜率,计算y坐标
                    double y = k * (i - pointMinX.X) + pointMinX.Y;
                    // 简单判断一下y是不是整数
                    double d = y - (int)y;
                    if (0.001 > d && d > -0.001)
                    {
                        linePoint.Add(new Point(i,(int)d));
                    }
                }
                //打印点
                foreach (var item in linePoint)
                {
                    Console.WriteLine(item.X + @"," + item.Y);
                }
    

     画线

    依据某个端点,找出这个端点方向的下一个三个周围点,找出最符合斜率的点。

    public bool DrawLineOnPicture(Color c, Point start, Point end)
            {
                if (MyBitmapData == null) { return false; }
                int x, y;
                int dstStride = MyBitmapData.Stride;
                //Object thisLock = new Object();
                //lock (thisLock){
                System.IntPtr dstScan0 = MyBitmapData.Scan0;
                try
                {
                    if (start.X > end.X)
                    {
                        Point temp = start;
                        start = end;
                        end = temp;
                    }
                    int step;
                    if (start.Y < end.Y)
                        step = 1;
                    else
                        step = -1;
                    x = start.X;
                    y = start.Y;
                    unsafe
                    {
                        byte* pDst = (byte*)(void*)dstScan0;
    
                        while ((x != end.X) || (y != end.Y))
                        {
    
                            pDst[x * 3 + y * dstStride] = c.R;
                            pDst[x * 3 + y * dstStride + 1] = c.G;
                            pDst[x * 3 + y * dstStride + 2] = c.B;
    
                            Point p1 = new Point(x + 1, y);
                            Point p2 = new Point(x, y + step);
                            Point p3 = new Point(x + 1, y + step);
    
                            double distance1 = DistanceToLine(p1, start, end);
                            double distance2 = DistanceToLine(p2, start, end);
                            double distance3 = DistanceToLine(p3, start, end);
    
                            double distance = Math.Min(Math.Min(distance1, distance3), distance2);
                            if (distance == distance1)
                            {
                                x = p1.X;
                                y = p1.Y;
                            }
                            else if (distance == distance2)
                            {
                                x = p2.X;
                                y = p2.Y;
                            }
                            else
                            {
                                x = p3.X;
                                y = p3.Y;
                            }
                        }
                    }
                }
                catch (Exception )
                {
                    //DBConnection.LevelTrace(TraceLevel.Error, "MyImage-->DrawLineOnPicture failed with:" + e);
                    return false;
                }
                //}
                return true;
            }
    private double DistanceToLine(Point p, Point lineStart, Point lineEnd)
            {
                if (lineStart.X == lineEnd.X)
                    return (Math.Abs(p.X - lineEnd.X));
                else if (lineStart.Y == lineEnd.Y)
                    return (Math.Abs(p.Y - lineEnd.Y));
                else
                {
                    double a = 1.0 * (lineEnd.Y - lineStart.Y) / (lineEnd.X - lineStart.X);
                    double b = lineStart.Y - a * lineStart.X;
                    double b2 = p.Y - a * p.X;
                    return (Math.Abs(b2 - b));
                }
            }
    
  • 相关阅读:
    【林】Ubuntu下安装和设置 OpenSSH Server
    吐吐槽
    【不定时推荐】这些年读过的书第一本--《一个人的朝圣》
    source insight 和keil 编辑对齐
    WeifenLuo DockContent停靠窗口的大小设置
    指针无法保存值
    php 练习基础
    php 写日志
    数据库设计——字段类型设计
    .net用TreeView实现CheckBoxes全选 反选 子节选中 传值
  • 原文地址:https://www.cnblogs.com/gaara-zhang/p/9140776.html
Copyright © 2011-2022 走看看