Solved

Draw Rectangle/Calculate Coordinates

Posted on 2013-12-26
4
448 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now