6
Answers

How to read a painted area on a form (really hard to describe in a line,please enter for more details)

Photo of silver lord

silver lord

14y
3k
1

Hi,
I have a form with a control which holds the task of showing a graph,by reading (x,y)s from a file and adding them points collection of a graph. now i want to write some code which which helps me do  the following:
when i choose a rectangle area containing some part of the graph or function on the form, a pop up form should come up and  show me the Fourier transform (or anyother transform) of the graph.
the biggest problem here is that i don't know how to extract some part of graph from the control. i mean it should give me all (x,y)s that form the graph or at least two (x,y)s that are first point and end point of graph( and then i will extract other (x,y)s from the information file). how should i do this?
any help appreciated
thanks

Answers (6)

1
Photo of PJ Martins
NA 3.8k 27.2k 14y
You'll need to create some type of dictionary that contains the translation of your x,y coordinates to the pixels on the screen, based on this you could find which pixels are selected in your rectangle and see if any of them are contained in your variable to get it's translated x and y coordinates. How is the graph drawn?
Accepted
1
Photo of PJ Martins
NA 3.8k 27.2k 14y

Do you need the two pieces to be gathered into two individual variables? If you simply need a list of points try this:
List<Point> containingPoints = new List<Point>();
for (int i = rect.Left; i <= rect.Left + rect.Width; i++)
{
   for (int j = rect.Top; j <= rect.Top + rect.Height; j++)
   {
      if (points.ContainsKey(new Point(i, j))
         containingPoints.Add(points[new Point(i, j)]);
   }
}
1
Photo of PJ Martins
NA 3.8k 27.2k 14y
You're on the right track, use a Dictionary<Point, Point> where your key is the left (x) and top (y) of your pixel and the second is the x and y point of your graph, then find the corresponding entry just as you described by the that falls within the range. If you're trying to get every point within your rectangle you'd have to loop through each pixel contained inside the rectangle to find it's corresponding graph points (if they exist). Let me know if that guides you on the right direction.
0
Photo of silver lord
NA 5 0 14y

again Hi PJ,

That brings another question and that is how i know that a point (as a key) exists in a rectangular area? 
(sorry about the word outside in the image)
28052010136.jpg


as you see in the picture someone may select a part of graph in way that breaks the graph into 2 parts. i'm completely new to gdi+ so i'm really confused. how can i save a rectangular area and how can i determine if a point is inside that rectangle. can i use this code (rec is selected rectangle) :

foreach( KeyValuePair<Point, Point> kvp in myDictionary )

       if (kvp.Key.Left > rec.Left && kvp.Key.Left < rec.Right  && 
              kvp.Key.Top < rec.Top && kvp.Key.Top > rec.Bottom)

              {

                       kvp.Value is a selected graph (x,y) pair
                }


any notes on this code? and error to be corrected?
Thanks
0
Photo of silver lord
NA 5 0 14y

Hi PJ,
thank you for your help. graph is drawn using zedgraph control available at http://zedgraph.org , so you say i must modify the control so the control returns a dictionary of (key=pixel , value=(x,y)) and when the MouseDown and MouseUp events occur, i must extract the part of dictionary that respond to pixels in MouseDown and MouseUp. can you please give me some advice on tecnical points?
0
Photo of Dushan Stankovic
NA 342 0 14y
You should override OnPaint method if you want to customize control, or set event handler for Paint event for form. In both methods just call:

//it's graphic area of control or form for drawing and painting
Graphics g = e.Graphics;

//Reclagle of drawing area
g.ClipBounds     

//methods for drawing elements
g.DrawLine DrawString DrawRectangle  .......

//methods for filling elements with you Brushes
g.FillRectangle FillElipse .....