Click here to Skip to main content
15,914,323 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a form which have two lines on it just like the two coordinates x-axis and y-axis
Now i want to add points on these coordinates.

e.g on this point(2,3) i want to add a point
on the point (-4,5) another point
on the point (-5,-7) another point
on the point (4,-6) another point

How can i do this?

here are two lines

VB
e.Graphics.DrawLine(Pens.Black, 190, 210, 350, 210)
    e.Graphics.DrawLine(Pens.Black, 330, 90, 330, 300)
Posted
Comments
Bernhard Hiller 14-Jul-14 6:30am    
Where is your problem:
- transforming the coordinates?
- drawing a "symbol" at the location of the point?
Yellow_Flash 15-Jul-14 0:23am    
transforming coordinates
Yellow_Flash 15-Jul-14 0:31am    
and also drawing + symbol on the points

Use simple mathematics. You must store your original 0,0 point location on form. Assuming your form has dimensions: 300x200 and you have your axis exactly in the middle of the form you can do like this:

C#
private int X0 = 150;
private int Y0 = 100;

// Pass your values to calculation method
private Point CalculatePoint(int x, int y)
{
    return new Point(X0 + x, Y0 + y);
}


Then in your code you call this method like this:

C#
var point1 = CalculatePoint(2, 3);    // 152, 103
var point2 = CalculatePoint(-4, 5);   // 146, 105
var point3 = CalculatePoint(-5, -7);  // 145, 93
var point4 = CalculatePoint(4, -6);   // 154, 94


You can use form's OnClientSizeChanged to determine when form size changed. Then simply update your original point location:

C#
private void Form1_ClientSizeChanged(object sender, EventArgs e)
{
    X0 = this.Size.Width / 2;
    Y0 = this.Size.Height / 2;
}


And don't forget to redraw your chart.

Cheers!
 
Share this answer
 
Comments
Yellow_Flash 15-Jul-14 0:30am    
thanks this gives me a lot of help in my solution
Marcin Kozub 15-Jul-14 6:42am    
Glad I could help you :)
easiest way is following step as I think

1) Add control array of label if number of dots are not fixed.
2) Add two textbox for co-ordinates entry e.g. textX, textY
3) Add in there textchanged event, enter following code
VB
textX.location = New System.Drawing.Point(Val(txtX.Text) , textX.location.Y)

VB
textY.location = New System.Drawing.Point(textX.location.X,Val(txtY.Text))


try this and reply.
 
Share this answer
 
Comments
Yellow_Flash 15-Jul-14 0:14am    
actually this is not what i am looking for
I can't use any labels and text boxes
my data for coordinates is coming from a gridview and on the point where they intersect i want to draw a string there
e.g 4,5 and on this point i want to add a string "7"
nilesh sawardekar 15-Jul-14 4:52am    
ok...anyway thank you for reply

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