Solved

Draw Rectangle/Calculate Coordinates

Posted on 2013-12-26
4
453 Views
Last Modified: 2013-12-27
I have been searching for a few days now and have been unable to find an answer.

I would like to be able to draw rectangle(s) during runtime in my application.

After the rectangle is placed, I am hoping that it would be possible to calculate the coordinates of the corners.

The rectangles will be over a picturebox.

Is this even possible?
0
Comment
Question by:SolveEtCoagula07
  • 2
  • 2
4 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 39741345
Well, this is a strange question. Because you are the responsible to draw the rectangles over the picture box, you must know the coordinates before drawing. So, if you know the coordinates, why do you want to calculate them later?

Please, can you explain better what are you trying to do?
0
 
LVL 1

Author Comment

by:SolveEtCoagula07
ID: 39741347
Ah yes....sorry about that.  I want the user to draw it by clicking and then dragging the mouse.
0
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 39741372
Well, even in that case you'll need to control the MouseDown and MouseUp events over the PictureBox, in the MouseDown event you'll need to store the initial coordinate (Point) and in the MouseUp event the final coordinate, so when you draw the rectangle you know exactly the coordinates in which you are drawing it. There's no need to calculate them later.

Calculate the coordinates after drawing is not trivial and depends on the conditions in which the rectangle was drawn. Consider, for example, the rectangle in the rectangle1.png attached file.One-pixel width rectangle black over white.
If you want to calculate the corners in this rectangle you can do easily. Look at the red points (I've enclosed them into circles). Any point in the horizontal lines of the rectangle will have coloured points only to the left and to the right. Also any point in the vertical lines will have coloured points up and below. But the corners are different. For example, the upper-left corner has a coloured point to the right and another one below. So you can (more or less) easily search for the corners looking for these patterns.

But the whole thing complicates if the rectangle is not 1-pixel width, or if it's anti-aliased (not all points have the same color), etc.

Hope that helps.
0
 
LVL 1

Author Closing Comment

by:SolveEtCoagula07
ID: 39742272
Your answer made me think about something and it came to me.  I was able to use the two X and Y coord pairs(top left, bottom right) to calculate the other corners.  I feel that I should be able to complete the rest of this project soon.  Thank  you!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question