如何使用自定义坐标绘制线条或点 [英] how to use custom coordinates for drawing lines or points

查看:127
本文介绍了如何使用自定义坐标绘制线条或点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi all,
I am new at GDI and graphics and will like some help trying to draw lines or plot points.
I have the following code and do not understand why the line is not drawn? 

using System;
using System.Drawing;
using System.Windows.Forms;
namespace lineplot
{
    public partial class Form1 : Form
    {
        private Rectangle drawingRectangle;
        private float xMin = 500000f;
        private float xMax = 600000f;
        private float yMin = 5000000f;
        private float yMax = 6000000f;
        private int offset = 10;

        public Form1()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.BackColor = Color.White;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Rectangle rect = ClientRectangle;
            drawingRectangle = new Rectangle(rect.Location, rect.Size);
            drawingRectangle.Inflate(-offset, -offset);
            g.DrawRectangle(Pens.Red, rect);
            g.DrawRectangle(Pens.Black, drawingRectangle);
            Pen aPen = new Pen(Color.Green, 3);
            g.DrawLine(aPen, Point2D(new PointF(695312, 5668062)),
                Point2D(new PointF(695312, 5668100)));
            aPen.Dispose();
            g.Dispose();
        }

        private PointF Point2D(PointF ptf)
        {
            PointF aPoint = new PointF();
            aPoint.X = drawingRectangle.X + (ptf.X - xMin) *
                drawingRectangle.Width / (xMax - xMin);
            aPoint.Y = drawingRectangle.Bottom - (ptf.Y - yMin) *
                drawingRectangle.Height / (yMax - yMin);
            return aPoint;
        }
    }
}<pre lang="c#">

推荐答案

全部看起来是正确的,不计算代表坐标及其疯狂值的硬编码立即常量。从简单的东西开始,显然正确绘制,只是为了检查自己,然后修改代码来绘制你想要的东西。



你甚至可以缩小现有的渲染,看看是什么它是否规模较小。如果你想这样做,请使用

https://msdn.microsoft.com/en-us/library/system.drawing.graphics.transform%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.matrix%28v=vs.110%29.aspx [ ^ ]。br />


您还可以使用 Matrix 和此属性进行简单的坐标转换



-SA
All looks correct, not counting hard-coded immediate constants representing coordinates and their crazy values. Start with something simple which apparently draws correctly, just to check yourself, and then modify code to draw what you want.

You can even scale existing rendering down to see what is it in smaller scale. If you want to do it this way, use
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.transform%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.matrix%28v=vs.110%29.aspx[^].

You can also use Matrix and this property for simple coordinate transform.

—SA


这篇关于如何使用自定义坐标绘制线条或点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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