-3

How do I draw a line from one end to another, how do I control it's length and direction?

I have been testing by changing values but it's giving me random results, I do not want to guess, I want to understand the concept so I know what I'm doing.

This is my code below.

 Pen blackPen = new Pen(Color.Black, 1);
 Point p1 = new Point(20, 30);
 Point p2 = new Point(10, 10);
 e.Graphics.DrawLine(blackPen,p1,p2);

Please let me know if you need any more information. thank you.

enter image description here

EDITED:

Apologies for such a broad question:

I have used this code and this draws a line from bottom to across the screen, I want to reduce it's length that is going upwards. please use this code.

graphics is in mm.

  Pen blackPen = new Pen(Color.Black, 1);
  Point p1 = new Point( 110, 80 );
  Point p2 = new Point(240 , 20 );
Fawad Naseer
  • 35
  • 10
  • What are your "ends"? You may want to use `e.Graphics.Clip` to determine the size of the `Graphics` your drawing on. – René Vogt Jul 19 '16 at 14:08
  • I'm unclear on what your question is. If you want to understand more than just copy and paste code then the docs are a good place to start. Eg https://msdn.microsoft.com/en-us/library/f956fzw1(v=vs.110).aspx . If there are specific questions you still have after reading them then best to then actually ask them here. I highly doubt you are getting "random results" in your experimentation but without seeing the code you were using and the results you got (along with telling us what you expected) then its very hard for us to tell you where you went wrong and what your misunderstandings might be. – Chris Jul 19 '16 at 14:16
  • 1
    Your code draws a line between p1 and p2 Your points consist of one x and one y coordinate each. Point p1 = new Point(20, 10); Point p2 = new Point(10, 10); Would draw a horizontal line since the y coordinate is the same – Jonny Jul 19 '16 at 14:20
  • @Jonny and what does X1 and X2 will do? – Fawad Naseer Jul 19 '16 at 14:39
  • You have x-coordinates along the horizontal line of your screen and y-coordinates along the vertical line of your screen. – Jonny Jul 19 '16 at 15:07
  • _"how do I control it's length and direction"_ -- the line is determined precisely by the two points you pass to the `DrawLine()` method. You control the line's length and direction by selecting appropriate points. At best, your question as asked is too broad, and frankly is not very clear at all. If you want to know how to compute the second point given the first, an angle, and a direction, then that's just a math problem. See e.g. https://stackoverflow.com/questions/3545059/how-to-calculate-the-pointx2-y2-with-the-point-x1-y1-and-the-angle – Peter Duniho Jul 19 '16 at 15:07

2 Answers2

1

DrawLine connects two points in space.

Screen coordinates [X,Y] 

[0,0] [1,0] [2,0] [3,0]
[0,1] [1,1] [2,1] [3,1]
[0,2] [1,2] [2,2] [3,2]
[0,3] [1,3] [2,3] [3,3]
[0,4] [1,4] [2,4] [3,4]

So if you type DrawLine(pen, new Point(0, 2), new Point(2, 4)); you will get:

[0,0] [1,0] [2,0] [3,0]
[0,1] [1,1] [2,1] [3,1]
===== [1,2] [2,2] [3,2]
[0,3] ===== [2,3] [3,3]
[0,4] [1,4] ===== [3,4]
apocalypse
  • 5,764
  • 9
  • 47
  • 95
0

X is used to specify a horizontal position. 0 is the left most position and the right most position is dependant on your screen size (resolution). If you have a window or a canvas of some sort the same applies.

Y is used to specify a vertical position. 0 is the top most position of your screen and the bottom most position is also dependant on your screen size (resolution).

If you have a screen 1920 x 1080 pixels, here are a few examples

This will draw a black line from your top left corner to the bottom right corner

 e.Graphics.DrawLine(new Pen(Color.Black, 1),new Point(0,0), new Point(1919, 1079));

This will draw a green line ten pixels down from the top. Horizontally across your screen.

 e.Graphics.DrawLine(new Pen(Color.Green, 1),new Point(0,10), new Point(1919, 10));

And this will draw a red line horizontally across the middle of your screen

 e.Graphics.DrawLine(new Pen(Color.Red, 1),new Point(0,540), new Point(1919, 540));

Finally this will draw a blue line vertically across the middle of your screen

 e.Graphics.DrawLine(new Pen(Color.Blue, 1),new Point(960,0), new Point(960, 1079));

For your additional question

Pen blackPen = new Pen(Color.Black, 1);
Point p1 = new Point( 110, 80 );
Point p2 = new Point(240 , 20 );

You are drawing from 110, 80 to 240,20. So you're starting 80 pixels down and drawing to a point 60 pixels above it (20).

Change

    Point p2 = new Point(240 , 20 );

to

    Point p2 = new Point(240 , 40 );

and see the right end move down.

Jonny
  • 1,037
  • 7
  • 15