Click here to Skip to main content
15,399,444 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
[Edited 3/17/2014-13:00 reason: Unclear question.]

Hey Codeproject,

I have been using the Graphics Path it's a really helpful class, however I am facing a problem.

I am drawing several rectangles using another graphics part using cooardinates that are randomly generated. Inbetween those rectangles(Imagine a 9 by 9 field, and the position(3,3) being inbetween) I want to draw another rectangle, or a triangle, depending on the environment without affecting the other rectangles.

The rectangles are completely seperated from the "morphing" rectangle, but can be aquired by the list of points I've made.

List<Point> Rectangles = new List<Point>();
int RectangleSize = 32;
for(int i =0; i<3; i++)
 for(int j =0; j<3; j++)
    Rectangles.Add(i * RectangleSize, j * RectangleSize); 
    Rectangles.Add(i * RectangleSize + RectangleSize, j * RectangleSize); 
    Rectangles.Add(i * RectangleSize + RectangleSize, j * RectangleSize + RectangleSize); 
    Rectangles.Add(i * RectangleSize, j * RectangleSize + RectangleSize); 

What I am attempting to do is find out the neighbouring rectangles, and create a polygon to connect to neighbouring rectangles. I've only so far managed to use triangles, and add them manually and have so far not come up with any type of algorithm to determine the points.

Here's a sample of the method I am looking for, also in the picture you can see "Hitpoints" I need to be able to recursively make points inbetween those to smoothen the lines to something more curvy:
public void Polygonize(List<Point> Neighbours, int Range)
 List<Point> Polygon = new List<Point>();

  // Create a polygon in the middle of all the points, by adding points closest to the middle, and not any further than the range(Which would be the rectangle size

 Neighbours = Polygon;

The list as from the picture would look like this:
new Point(0, 0),
new Point(32, 32),
new Point(0, 32)

Which would result as a triangle. I'm hoping to be able to add more points(recursively), to make it more curvy.

Here's the picture:

Is this possible?
Updated 17-Mar-14 2:07am
Sergey Alexandrovich Kryukov 16-Mar-14 14:42pm
Not clear. Do you mean System.Drawing.Drawing2D.GraphicsPath, or something else? What does it mean, "attach"? And so on...
Deviant Sapphire 16-Mar-14 15:22pm
I'm sorry for not be clearing enough, I happened to have just fallen into my coding weeks again(Been up for 36 hours now coding minute after minute.)
Yes that's exactly the one I am using, and with attaching I mean being able to make it look like it's one whole model/sprite/texture, without affecting any of the actual objects/textures.
Sergey Alexandrovich Kryukov 16-Mar-14 18:44pm
No problem. You simply need to move both object by the same distance/direction relative to their initial location. Move it you your data and invalidate the control where you show them...
Deviant Sapphire 16-Mar-14 15:22pm
I really need your email by the way, you happen to help me every time. :)
Sergey Alexandrovich Kryukov 16-Mar-14 18:38pm
Please, let's communication through this site. If you want, you can contact me through my site you can find through my CodeProject profile, but I cannot guarantee anything...
BillWoodruff 17-Mar-14 1:39am
If you have been "up for 36 hours," you certainly need to get some sleep ! After a rest, please describe your problem again: you mention a polygon, a triangle, and a rectangle. Is the triangle the polygon ?

By "attach," do you mean to "align" so that two parallel side overlap ? If you mean "align," you need to be very specific about exactly what that is.
Deviant Sapphire 17-Mar-14 7:47am
Okay, I've just woken up and have been to school. I just realised how unclear my question really was. I'll edit it now, and hope it will be more clear.

- Dimitri

1 solution

A GraphicsPath is merely a collection of lines and curves that can be accessed through the GraphicsPath rather than individually.

The following code is the event handler for the Paint event. This handler can be dropped into any WinForm Form application. When the event is raised, the handler performs two major actions.

  1. A rectangle is declared, drawn, and filled; a triangle is declared, drawn, and filled. The drawing and filling is of the shapes themselves.
  2. A GraphicsPath is declared; a rectangle is declared and added to the GraphicsPath; a triangle is declared and added to the GraphicsPath (both the rectangle and the triangle are offset 150 pixels downward); the GraphicsPath is drawn and filled.

// *************************************************** OnPaint

protected override void OnPaint ( PaintEventArgs e )
    GraphicsPath    graphics_path;
    Rectangle       rectangle;
    Point [ ]       triangle = new Point [ 4 ];

    base.OnPaint ( e );

    this.Width = 200;
    this.Height = 300;

    rectangle = new Rectangle ( 10, 10, 100, 100 );
    e.Graphics.DrawRectangle ( Pens.Red, rectangle );
    e.Graphics.FillRectangle ( Brushes.Red, rectangle );

    triangle [ 0 ] = new Point ( 120,  60 );
    triangle [ 1 ] = new Point ( 180,  10 );
    triangle [ 2 ] = new Point ( 180, 110 );
    triangle [ 3 ] = new Point ( 120,  60 );
    e.Graphics.DrawLines ( Pens.Red,triangle );
    e.Graphics.FillPolygon ( Brushes.Red, triangle );

    graphics_path = new GraphicsPath ( );
                                // offset eveything down 150
    rectangle = new Rectangle ( 10, 160, 100, 100 );
    graphics_path.AddRectangle ( rectangle );

    triangle [ 0 ] = new Point ( 120, 210 );
    triangle [ 1 ] = new Point ( 180, 160 );
    triangle [ 2 ] = new Point ( 180, 260 );
    triangle [ 3 ] = new Point ( 120, 210 );
    graphics_path.AddLines ( triangle );

    e.Graphics.DrawPath ( Pens.Blue, graphics_path );
    e.Graphics.FillPath ( Brushes.Blue, graphics_path );

    graphics_path.Dispose ( );

GraphicsPaths are somewhat easier to use than Regions. Remember to Dispose of the GraphicsPath when done with it.

Hope that helps.

Deviant Sapphire 17-Mar-14 8:08am
I appreciate your answer and from reading my question this would have been the answer, but I didn't ask it properly. Thanks a lot for your input though!
gggustafson 17-Mar-14 11:47am
Still quite unclear. Is this a school project? If so, you could supply the project's requirements (i.e., assignment). Hopefully that might be more clear.
Deviant Sapphire 17-Mar-14 12:18pm
It's not an assigment, it's a project I've been working on for quite some years now. I finally found the example I wanted to show you guys.

It's a 2D version of this:
gggustafson 17-Mar-14 12:59pm
I think your question is a little misleading. Your problem is not with GraphicsPath but rather "so far not come up with any type of algorithm to determine the points." It is that algorithm that is the problem. Also, if your project is used in Minecraft, I'd suggest that you add Games to the tags.

Hope you find your 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