Search code examples
c#winformsmovedrawrectangledrawellipse

Move an Ellipse in angular direction


hey there I am new to C# Graphics Programming. I need to know how can I move an Ellipse inside my windows form in angular directions. I have been successfully moved my Ellipse in default direction using my code.


My Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Paddle_Test
{
    public partial class Form1 : Form
    {
        Rectangle rec;
        int wLoc=0;
        int hLoc=0;
        int dx=3;
        int dy=3;

    public Form1()
    {
     InitializeComponent();
     rec = new Rectangle(wLoc,hLoc , 100, 10);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.Refresh();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.FillEllipse(new SolidBrush(Color.Blue), rec);

    }

    private void timer_Tick(object sender, EventArgs e)
    {
        //moving inside my timer
        rec.X += dx;  
        rec.Y += dy;  
    }


}
  }

In simple words my Ellipse is only moving diagonally! So the Question in simple words is is it possible for me to move it like 30' or 80' or Angle Specified!


enter image description here


Solution

  • I believe you're looking for some basic trigonometry functions, like:

    x = cos(degrees) * maxX;
    y = sin(degrees) * maxY;