Updated descendant tree
User Rating: / 0
- Details
-
Category: Development
-
Published on Tuesday, 25 June 2013 14:20
-
Written by Ove
-
Hits: 3880
When we program 2D graphics with many objects that will rotate, move and be zoomed in and out it can be quite complex to find what object the user clicked. Matrices is the way to go in many cases and i will show how i find out what object the user clicked. The tool i use is Microsoft's Visual C# 2008 Express and we will just rotate the objecs here but it will work fine with any movement and zooming also.
To rotate the object around its own center we can do this.
Rectangle rect = new Rectangle(object.Location, new Size(object.Width, object.Height));
Matrix m = new Matrix();
m.RotateAt(25, new PointF(rect.Left + rect.Width / 2, rect.Top + rect.Height / 2));
Graphics.Transform = m;
Graphics.DrawImage(object, rect);
Then the object will be rotated 25 degrees and the same Matrix can be used to rotate thousands of other objects. To find out if the user clicked on a point that is inside the object we just get the point (p) that the user clicked on and do this:
Point[] pts = new Point[1];
pts[0] = p;
m.Invert();
m.VectorTransformPoints(pts);
pts[0].X += (int)m.OffsetX;
pts[0].Y += (int)m.OffsetY;
Now pts[0] can be used to find if that object was clicked with this code:
if (rect.Contains(pts[0]) MessageBox.Show("You clicked inside");
***************************************************************************
I have now updated DescendantView to use this technique and it rotates very nice the names and dates will be rotated locally the other way so the names are shown horisontally all the time. Rotating the descendant tree 180 degrees was very nice....the youngest persons will be at top then...
27.06.2013 - Updated it with a selection border around clicked person as shown here:
By the way here is a pic of the moon yesterday seen from my moms place