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?
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);
    Poin开发者_如何学JAVAt 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 middleForm = Form1.Height / 2 and diamondMiddleOfTheForm = middleForm - middle
I apologize for my mistake, if I did any in implementing what you said...
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);
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论