Click here to Skip to main content
15,906,626 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is my code to create polygon.
I was asked to draw an offset polygon.
If a user enters "50" in text box then a polygon should be drawn with extension of input
in all sides.

Here is my code

C#
Brush drawing_brush = new SolidBrush(Color.Red);

Brush drawing_brush1 = new SolidBrush(Color.Blue);

//Creating Pen

Pen drawing_pen = new Pen(drawing_brush, 2);
Pen drawing_pen1 = new Pen(drawing_brush1, 2);


// Initiating Graphics


Graphics Draw = this.CreateGraphics();



//int x=300;
//int y=300;


int x = Convert.ToInt32(Width.Text);
int y = Convert.ToInt32(Height.Text);

//Points Initialization

Point P1 = new Point(x, y);
Point P2 = new Point(x + 100, y);
Point P3 = new Point(x + 100, y + 100);
Point P4 = new Point(x, y + 100);


Point[] P = { P1, P2, P3, P4 };
Draw.DrawPolygon(drawing_pen, P);


Point Q1 = new Point(x - 10, y - 10);
Point Q2 = new Point(x + 110, y - 10);
Point Q3 = new Point(x + 110, y + 110);
Point Q4 = new Point(x - 10, y + 110);
Point[] Q = { Q1, Q2, Q3, Q4 };



Draw.DrawPolygon(drawing_pen1, Q);
Posted
Updated 6-Feb-14 22:12pm
v3
Comments
Kenneth Haugland 7-Feb-14 2:51am    
Yes
KUMAR619 7-Feb-14 2:58am    
Please tell me how to create a polygon using user defined input in c#
Per Söderlund 7-Feb-14 3:06am    
You have to give us more information, what have you tried and where did it fail?
Also, stop using Caps Lock.
OriginalGriff 7-Feb-14 3:09am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
BillWoodruff 7-Feb-14 4:15am    
This is a repost (kind-of) of a question you posted 2 hours ago:

http://www.codeproject.com/Questions/722521/How-to-create-Offset-Polygon-using-Csharp

 
Share this answer
 
Yes, for instance, the Graphics class provides the DrawPolygon[^] method. You could ask the user to input the required points and then use such method to perform the task.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900