如何在一个边缘到另一个边缘之间找到矩形内的距离并在两个边缘之间画一条线? [英] How Do I Find The Distance Inside A Rectangle Between One Edge To Another And Draw A Line Between The Two Edges ?

查看:72
本文介绍了如何在一个边缘到另一个边缘之间找到矩形内的距离并在两个边缘之间画一条线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:



I have this code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace mws
{
    public partial class PaddingPoints : Form
    {
        private Point point;
        private Rectangle _mrect;
        private List<PointF> points = new List<PointF>();
        private Point RectStartPoint;
        private Image img;
        private Image imgClone;
        private Pen myPen;
        private int n = 37; //number of points
        private Bitmap _bmpBU = null;

        public PaddingPoints()
        {
            InitializeComponent();

            this.DoubleBuffered = true;
            _bmpBU = new Bitmap(@"D:\MyWeatherStation-Images-And-Icons\radar090.PNG");

            myPen = new Pen(Brushes.Red, 2);
            //Bitmap to hold the picturebox image
            img = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            Graphics g;
            using (g = Graphics.FromImage(img))
            {
                g.DrawImage(_bmpBU, 0, 0, pictureBox1.Width, pictureBox1.Height);
            }

            //image to hold the original picturebox. We need it to clear img to the original 
            //picturebox image
            imgClone = (Bitmap)img.Clone();

            //We draw always on img and then we invalidate
            pictureBox1.Image = img;
        }

        private void PaddingPoints_Load(object sender, EventArgs e)
        {

        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            RectStartPoint = e.Location;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            label12.Text = string.Format("X = {0} Y = {1}", e.Location.X, e.Location.Y);
            if (e.Button == MouseButtons.Left && e.Location != RectStartPoint)
            {
                label2.Text = points.Count.ToString();
                label6.Text = points.Count.ToString();
                label7.Text = points.Count.ToString();
                label8.Text = points.Count.ToString();
                DrawRectangle(e.Location);
            }
        }

        private void DrawRectangle(Point pnt)
        {
            PointF pointf = new PointF(0, 0);
            points = new List<PointF>();
            Graphics g = Graphics.FromImage(img);
            int width, height, i, x, y;

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            //Clear img from the rectangle we drawn previously
            g.DrawImage(imgClone, 0, 0);


            if (pnt.X == RectStartPoint.X || pnt.Y == RectStartPoint.Y)
            {
                g.DrawLine(myPen, RectStartPoint.X, RectStartPoint.Y, pnt.X, pnt.Y);
            }
            else
            {
                g.DrawRectangle(myPen, Math.Min(RectStartPoint.X, pnt.X), Math.Min(RectStartPoint.Y, pnt.Y),
                                Math.Abs(RectStartPoint.X - pnt.X), Math.Abs(RectStartPoint.Y - pnt.Y));
                //width of spaces between points
                width = (int)((Math.Abs(RectStartPoint.X - pnt.X)) / (n - 1));
                //height of spaces between points
                height = (int)((Math.Abs(RectStartPoint.Y - pnt.Y)) / (n - 1));
                //we always want the upper left x, y coordinates as a reference drawing clockwise
                x = Math.Min(RectStartPoint.X, pnt.X);
                y = Math.Min(RectStartPoint.Y, pnt.Y);

                //Drawing the points. change the 3, 6 values for larger ones
                for (i = 0; i < n - 1; i++)
                {
                    //Up side
                    g.FillEllipse(Brushes.Green, new Rectangle(x - 3, Math.Min(RectStartPoint.Y, pnt.Y) - 3, 6, 6));
                    //Right side
                    g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) + Math.Abs(RectStartPoint.X - pnt.X) - 3, y - 3, 6, 6));
                    //Bottom side
                    g.FillEllipse(Brushes.Green, new Rectangle(x - 3, Math.Min(RectStartPoint.Y, pnt.Y) + Math.Abs(RectStartPoint.Y - pnt.Y) - 3, 6, 6));
                    //Left side
                    g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) - 3, y - 3, 6, 6));
                    pointf = new PointF(Math.Min(RectStartPoint.X, pnt.X) - 3, y - 3);
                    points.Add(pointf);
                    x += width;
                    y += height;
                }
                g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) + Math.Abs(RectStartPoint.X - pnt.X) - 3,
                              Math.Min(RectStartPoint.Y, pnt.Y) - 3, 6, 6));
                g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) + Math.Abs(RectStartPoint.X - pnt.X) - 3,
                             Math.Min(RectStartPoint.Y, pnt.Y) + Math.Abs(RectStartPoint.Y - pnt.Y) - 3, 6, 6));
            }

            g.Dispose();

            //draw img to picturebox
            pictureBox1.Invalidate();
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            label10.Text = ((n - 1) * 4).ToString();            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                ScanBmpFast((Bitmap)this.pictureBox1.Image, true, _mrect);
                this.pictureBox1.Refresh();
            }
        }

        private unsafe void ScanBmpFast(Bitmap bmp, bool setToOpaqueYellow, Rectangle rect)
        {
            BitmapData b1 = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            int stride = b1.Stride;
            int nWidth = bmp.Width;
            int nHeight = bmp.Height;

            System.IntPtr Scan0 = b1.Scan0;

            byte* p = (byte*)(void*)Scan0;

            int sX = Math.Min(Math.Max(rect.X, 0), bmp.Width);
            int sY = Math.Min(Math.Max(rect.Y, 0), bmp.Height);
            int w = Math.Min(rect.Width, bmp.Width - sX);
            int h = Math.Min(rect.Height, bmp.Height - sY);

            for (int y = sY; y < sY + h; y++)
            {
                p = (byte*)(void*)Scan0;
                p += (y * stride + sX * 4);

                for (int x = sX; x < sX + w; x++)
                {
                    if (p[0] != 0 || p[1] != 0 || p[2] != 0)
                    {
                        p[1] = p[2] = (byte)255;

                        if (setToOpaqueYellow)
                        {
                            p[0] = (byte)0;
                            p[3] = (byte)255;
                        }
                    }

                    p += 4;
                }
            }

            bmp.UnlockBits(b1);
        }
    }
}







方法内部DrawRectangle当我移动鼠标时,我正在实时绘制一个红色的矩形。

现在我想在DrawRectangle方法中添加一个代码,当它绘制矩形时,它还会从左边缘中间到右边缘中间绘制/添加一条线。从顶部边缘中间到底部边缘中间的另一条线。



尝试了一些方法,但它没有用。



我找不到如何计算左右边缘,上下之间的距离以及如何在每两条边之间画一条线。



我不介意它会在鼠标向上事件中画出这些线条,或者只有当我点击一个按钮时才会这样做。以及如何在DrawRectangle方法中实时制作它。



有人可以告诉我,我应该更改/添加到此代码中吗?

DrawRectangle方法我没有这样做,但我将它用于其他事情,所以我需要保留它,但要在问题中添加我需要的内容。




Inside the method DrawRectangle i'm drawing a rectangle in red in real time when i'm moving the mouse.
Now i want to add a code to the DrawRectangle method that while it's drawing the rectangle it will also draw/add a line from the left edge middle to the right edge middle. And another line from the top edge middle to the bottom edge middle.

Tried some ways but it didn't work.

I couldn't find how to calculate the distance between the edges left and right, up and down and how to draw a line between each two edges.

I don't mind to make that it will draw this lines in the mouse up event or only if i click on a button. And also how to make it in the DrawRectangle method in real time.

Can someone show me please what should i change/add to this code ?
The DrawRectangle method i didn't do it but i'm using it for other things so i need to keep it but to add to it what i need in the question.

推荐答案

你有什么问题?小学中学几何学?你如何定义距离?两个相对边之间的最短距离是宽度或高度,而在非相对边之间它是零,因为它们相交。 :-)

如果用距离表示其他任何内容,请澄清。



-SA
What is your problem? Elementary middle-school geometry? How do you define the distance? Shortest distance between two opposite sides is Width or Height, and between non-opposite side it's zero, because they intersect. :-)
If by "distance" you mean anything else, please clarify.

—SA


假设我想在Panel中绘制十字线图形,我会通过创建一个由四个矩形组成的GraphicsPath来实现这一点,这些矩形以1个单位的方形定义:
Assuming I wanted to draw the "cross-hairs" graphic in a Panel, I would approach this by creating a GraphicsPath composed of four rectangles, defined in a 1-unit-square:
private GraphicsPath quadPath = new GraphicsPath();

private GraphicsPath copyQuadPath;

private Matrix quadMatrix;

private Pen quadPen;

private int mdX, mdY;

private bool isMouseUp = true;

private void Form1_Load(object sender, EventArgs e)
{
    quadPen = new Pen(Color.Red,1.0F);

    quadMatrix = new Matrix();

    quadPath.AddRectangle(new RectangleF(0.0F, 0.0F, 0.5F, 0.5F));
    quadPath.AddRectangle(new RectangleF(0.5F, 0.0F, 0.5F, 0.5F));
    quadPath.AddRectangle(new RectangleF(0.5F, 0.5F, 0.5F, 0.5F));
    quadPath.AddRectangle(new RectangleF(0.0F, 0.5F, 0.5F, 0.5F));
}

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    mdX = e.X;
    mdY = e.Y;
    isMouseUp = false;
}

private void panel1_MouseUp(object sender, MouseEventArgs e)
{
    isMouseUp = true;
}

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    // test to exit if mouse is not down (user not click-dragging)
    if (isMouseUp) return;
    
    // scale factors
    int dx = e.X - mdX;
    int dy = e.Y - mdY;
    
    // transform the matrix to scale and translate 'quadPath

    // reset the quadMatrix to the identity Matrix
    quadMatrix.Reset();

    // translate to where the mouse went down
    quadMatrix.Translate(mdX, mdY);

    // scale
    quadMatrix.Scale(dx,dy);

    // copy quadPath
    copyQuadPath = (GraphicsPath) quadPath.Clone();

    // transform it
    copyQuadPath.Transform(quadMatrix);
    
    // force a redraw of panel1
    panel1.Invalidate();
}

// Then, in the Paint Event of the Panel:

private void panel1_Paint(object sender, PaintEventArgs e)
{
    if (! isMouseUp) e.Graphics.DrawPath(quadPen, copyQuadPath);   
}

我已经对此进行了测试,并且(令我惊讶的是)它运行良好:)您可以将此方法的性能和视觉质量与您尝试过的其他方法进行比较。如果它不起作用,请告诉我。

I've tested this, and (to my surprise) it worked well :) It's up to you to compare the performance and visual quality of this approach to other approaches you've tried. If it doesn't work as well, please let me know.


这篇关于如何在一个边缘到另一个边缘之间找到矩形内的距离并在两个边缘之间画一条线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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