?
Solved

Draw Rectangle/Calculate Coordinates

Posted on 2013-12-26
4
Medium Priority
?
464 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
[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
  • Learn & ask questions
  • 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 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.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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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