1

When my polygon initially gets drawn after coming in contact with a 'Tag', it's drawn fine with the appropriate size and angles. However, when I rotate it, it gets distorted for eg. a right-angled triangle becomes an acute-like triangle.

So I have a feeling something is wrong with the RotateTransform bit I'm doing but I can't figure out what.

Original triangle drawn

Rotated triangle drawn

Here is my code for initializing values and rotating a polygon:

public Tangible(Point topLeft, Point topRight, Point bottomLeft, Point bottomRight, Point centerOfTag, BlobPair taglocation, String name, Double tagAngleOffset, String colour, String shapeType)
    {
        TopLeft = topLeft;
        TopRight = topRight;
        BottomLeft = bottomLeft;
        BottomRight = bottomRight;
        TagLocation = taglocation;
        Name = name;
        CenterOfTag = centerOfTag;
        TagAngleOffset = tagAngleOffset;
        Colour = colour;
        ShapeType = shapeType;
    }
public void draw (Canvas tagCanvas)
    {
        shapeOutline = new Polygon();

        //some other bits and pieces of code here
    }

public void update(Canvas tagCanvas)
    {
        Double width = BottomRight.X - BottomLeft.X;
        Double height = BottomLeft.Y - TopLeft.Y;
        Double shapeOutlineCentreX = TopLeft.X + (width / 2.0);
        Double shapeOutlineCentreY = TopLeft.Y + (height / 2.0);
        Double TagCentreOffsetX = CenterOfTag.X - shapeOutlineCentreX;
        Double TagCentreOffsetY = CenterOfTag.Y - shapeOutlineCentreY;

        Point position = TagLocation.BigBlob.GetCenterPosition(tagCanvas);
        Point otherPosition = TagLocation.SmallBlob.GetCenterPosition(tagCanvas);
        Vector v = otherPosition - position;
        Point centerPoint = position + v / 2;
        Double trigCenterX = CenterOfTag.X - TopLeft.X;
        Double trigCenterY = CenterOfTag.Y - TopLeft.Y;

        **shapeOutline.RenderTransform = new RotateTransform(Math.Atan2(v.Y, v.X) * 180 / Math.PI - TagAngleOffset, trigCenterX, trigCenterY);**

        shapeOutline.SetValue(Canvas.LeftProperty, centerPoint.X - (width / 2.0) - TagCentreOffsetX);
        shapeOutline.SetValue(Canvas.TopProperty, centerPoint.Y - (height / 2.0) - TagCentreOffsetY);
}
Naaz
  • 91
  • 1
  • 10
  • looks like this code is incomplete. You're missing the top half of your first method and the bottom half of the third method – Ben Aug 11 '15 at 23:23
  • 1
    It is recommended that you delete all this code and use proper XAML and DataBinding. – Federico Berasategui Aug 11 '15 at 23:25
  • 1
    That said, Make sure you set `RenderTransformOrigin` to `0.5,0.5` if you want to rotate with a centered axis. – Federico Berasategui Aug 11 '15 at 23:27
  • @HighCore what do you mean by use proper XAML and Databinding? – Naaz Aug 11 '15 at 23:28
  • @Naaz creating or manipulating UI elements in procedural code is discouraged in WPF, and goes against good practices. Also, the fact that you have a method called `Draw()` speaks loudly about the mentality used to write this code. In WPF, you don't "draw" anything. Instead, you create a proper strongly typed **Data Model** and then use **XAML** to declaratively define the UI and **DataBinding** to connect the declarative UI to the Data Model. This is a very different mindset to the one used in other technologies. More information [Here](http://stackoverflow.com/a/14382137/643085). – Federico Berasategui Aug 11 '15 at 23:33
  • Your image doesn't look like a right triangle is pictured. It's also hard to determine how the transform is effecting the shape because no axes are labeled. Also we can't tell when you're expecting to happen, the image looks it is rotated 90 degrees but we can't tell from what point of reference. Rotated from the center, a corner, one of the sides? – Kcvin Aug 12 '15 at 00:27

0 Answers0