Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Draw Rectangle/Calculate Coordinates

Posted on 2013-12-26
Medium Priority
465 Views
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
Question by:SolveEtCoagula07
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 25

Expert Comment

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

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

Luis Pérez earned 2000 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.
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

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

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…