I have to do application for tilling in C#. The tiles will have some shape and my app should be able to modify the shape. I will have a some shape – polygon, made from vertices. I will have for example field of 16 vertices, then I draw the polygon.
What I need to know how can I move with the the vertice using drag and drop. I will also have to recount other vertices in order to fit one tile to next tile, but it’s just some math.
To conclude:
I have polygon defined 16 vertices in field of Vertices, I move (with mouse) with one vertice, recount the coordinates of ohter vertices and draw new polygon. My problem is moving (probably using drag & drop) with one vertice from filed of vertices.
This is what part of my previous code without drag & drop – just for idea what tools I’m using for drawing one tile:
private Bitmap canvasBitmap; //canvas for drawing
private Graphics g; // enter to graphics tool
Bitmap b = (Bitmap)Bitmap.FromFile("obr.bmp");
TextureBrush brush = new TextureBrush(b);
Pen pen = new Pen(Color.Black, 1);
hexaVertices[0] = new PointF(-40 + 40, 0 + 30);
hexaVertices[1] = new PointF(-20 + 40, 30 + 30);
hexaVertices[2] = new PointF(20 + 40, 30 + 30);
hexaVertices[3] = new PointF(40 + 40, 0 + 30);
hexaVertices[4] = new PointF(20 + 40, -30 + 30);
hexaVertices[5] = new PointF(-20 + 40, -30 + 30);
g.FillPolygon(brush, hexaVertices);
g.DrawPolygon(pen, hexaVertices);
Thx for advices.
I can only give you a rough outline for Windows Forms here. In WPF you could use
Adorners and there are tutorials out there for how to do it. Here we go for a manual process in Windows Forms:First, the array of vertices should be a member variable of the class and should be initialized only once at the start of the program.
Then, draw the polygon with the current set of vertices as you’re doing right now. Also, draw some “handles” if you want, so you know that the vertices can be grabbed (this could be rectangles around the actual
PointF).Now for the magic 🙂 Assign the
MouseDown,MouseMoveandMouseUpevents to the control you’re using for displaying the image. Also, create a new member variablebool m_draggingVertexand another that contains the index to the vertex array of the vertex you’re currently dragging.In
MouseDown:Check whether the current mouse position is within the range of a vertext (I would assume a 5×5 rectangle around a vertex, so that is is easier to hit with the cursor). If you pressed the button on a vertex, set
m_draggingVertextotrueand store the index of the vertex in the other variable.In
MouseMove:If
m_draggingVertexistrue, change the vertext at the index stored above to the new coordinates, recalc your positions and repaint so that the current position of the vertex is shown.In
MouseUp:If
m_draggingVertexistrue, set it tofalseand do final work.This is how I’d do it …