Click here to Skip to main content
16,020,622 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: (untagged)
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#">
Posted
Comments
Member 10683902 26-Jun-15 18:58pm    
thanks, I tried the code with single digits and it works, not sure why it;s not working with the larger values. Once I can figure this out I'll use Point for storing the values.
Member 10683902 26-Jun-15 19:59pm    
Does X-min and Y-min values have to start from 0,0?

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900