0
votes

I have created a menu that allows users to choose the shape that they wanted to be drawn, then the user click on two points, band the chosen shape will be drawn on that point. I did the triangle this way. But I am not really sure how to draw a star using the same way.

class Triangle : Shape
{
    //This class contains the specific details for a triangle defined in terms of opposite corners
    Point keyPt, oppPt;      // these points identify opposite corners of the triangle

    public Triangle(Point keyPt, Point oppPt)   // constructor
    {
        this.keyPt = keyPt;
        this.oppPt = oppPt;
    }

    public void draw(Graphics g, Pen blackPen)
    {

        double xDiff, yDiff, xMid, yMid;   // range and mid points of x & y  

        // calculate ranges and mid points
        xDiff = oppPt.X - keyPt.X;
        yDiff = oppPt.Y - keyPt.Y;
        xMid = (oppPt.X + keyPt.X) / 2;
        yMid = (oppPt.Y + keyPt.Y) / 2;

        // draw triangle
        g.DrawLine(blackPen, (int)keyPt.X, (int)keyPt.Y, (int)(xMid + yDiff / 2), (int)(yMid - xDiff / 2));
        g.DrawLine(blackPen, (int)(xMid + yDiff / 2), (int)(yMid - xDiff / 2), (int)oppPt.X, (int)oppPt.Y);
        g.DrawLine(blackPen, (int)keyPt.X, (int)keyPt.Y, oppPt.X, oppPt.Y);
    }
}

Appreciate your help.

What did you try to draw a star? How would you calculate it? - Jeroen van Langen
Also: Do not use multiple call of DrawLine!! Instead collect to points and use DrawLines for a p0lyline or DrawPolygon for a closed shape!!! - Also: A neat trick for stars with n-points is to rotate the graphics object by 360f/n n times and drae on spike each time.. - TaW