views:

243

answers:

2

Hello all,

Here I have to create a diamond using drawlines method and make it move horizontally along a path that is half way from the top of the form.

I created a diamond and it is moving horizontally, but i want it to start moving from a position which is half way from the top of the form.

This is the code to create a diamond,

    private void Form1_Paint(object sender, PaintEventArgs e)
    {

        Graphics g = e.Graphics;
        Point p1 = new Point(5+x, 0);
        Point p2 = new Point(10+x, 5);
        Point p3 = new Point(5+x, 10);
        Point p4 = new Point(0+x, 5);
        Point[] ps = { p1, p2, p3, p4, p1 };
        Pen p_yellow = new Pen(Color.Yellow, 5);
        g.DrawLines(p_yellow, ps);
        this.BackColor = System.Drawing.Color.DarkBlue;
    }

I can make it move using the timer and following is the code,

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (x < 500)
            x += 2;
        else
            timer1.Enabled = false;
        this.Invalidate(); 
    }

please tell me how to bring the diamond to a point which is half way from the top of the form?

+2  A: 

you need to find the height of the diamond, first. take the highest point in the diamond: 0, and add the lowest point in the diamond: 10

height = 10

then find the middle of the diamond, vertically:

middle = height / 2

then find the middle of the form:

middleForm = form.Height / 2

then calculate the position of the diamond by moving it "up" from the middle of the form by half the height of the diamond:

diamondMiddleOfTheForm = middleForm - midddle

the "diamondMiddleOfTheForm" variable tells you where to offset your "y" values

        Point p1 = new Point(5+x, 0+diamondMiddleOfTheForm);
        Point p2 = new Point(10+x, 5+diamondMiddleOfTheForm);
        Point p3 = new Point(5+x, 10+diamondMiddleOfTheForm);
        Point p4 = new Point(0+x, 5+diamondMiddleOfTheForm);
Derick Bailey
Are you sure you want to use form.Height? I think it would be better if you used form.ClientRectangle.Height, so that it appears centered in the "working area", for lack of a better term.
APShredder
yes. i always forget about ClientRectangle. :)
Derick Bailey
oh! I never used that... i will try now...thanks for giving me a hint...
A: 

private void Form1_Paint(object sender, PaintEventArgs e) {

        int height = 10;
        int middle = height / 2;
        int middleform = Form1.height / 2;

        int diamondMiddleOfTheForm;
        diamondMiddleOfTheForm = middleForm - middle;

        Graphics g = e.Graphics;
        Point p1 = new Point(5 + x, 0 + diamondMiddleOfTheForm);
        Point p2 = new Point(10 + x, 5 + diamondMiddleOfTheForm);
        Point p3 = new Point(5 + x, 10 + diamondMiddleOfTheForm);
        Point p4 = new Point(0 + x, 5 + diamondMiddleOfTheForm);
        Point[] ps = { p1, p2, p3, p4, p1 };
        Pen p_yellow = new Pen(Color.Yellow, 5);
        g.DrawLines(p_yellow, ps);
        this.BackColor = System.Drawing.Color.DarkBlue;

    }

it shows an error at iddleForm = form.Height / 2 and diamondMiddleOfTheForm = middleForm - midddle

i apologize for my mistake, if I did any in implementing what you said...