Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7807239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:47:29+00:00 2026-06-02T02:47:29+00:00

Been rather busy posting today and creating an AI program to simulate Boids. Right

  • 0

Been rather busy posting today and creating an AI program to simulate Boids. Right now I am just working on getting a single “boids” drawing a moving around. The “boid” is just a circle(ellipsis) with a line draw to symbolize its “Forward Vector”.

class SwarmCir
{
    public float _pointX;
    public float _pointY;
    public float _height;
    public float _width;
    Pen _pen = new Pen(Color.Black);
    PointF _ForwardPoint = new PointF(0, 0);
    float rotAng = 0.0f;

    public SwarmCir()
    {
        _pointX = 1.0f;
        _pointY = 1.0f;
        _height = 5.0f;
        _width = 5.0f;
        _ForwardPoint.X = 7.0f;
        _ForwardPoint.Y = 7.0f;
    }
    public SwarmCir(Point XY)
    {
        _pointX = XY.X;
        _pointY = XY.Y;
        _height = 5.0f;
        _width = 5.0f;
    }
    public SwarmCir( Point XY, float Height, float Width )
    {
        _pointX = XY.X;
        _pointY = XY.Y;
        _height = Height;
        _width = Width;
    }
    public void SetPen(Pen p)
    {
        _pen = p;
    }
    public void Draw(Graphics g)
    {
        g.DrawEllipse(_pen, _pointX, _pointY, _width, _height);
        g.DrawLine(_pen, new PointF(_pointX, _pointY), _ForwardPoint);
    }
    public void Rotate(PaintEventArgs e)
    {
        e.Graphics.TranslateTransform(_pointX, _pointY);
        e.Graphics.RotateTransform(rotAng);
        e.Graphics.TranslateTransform(-_pointX, -_pointY);
    }
    public PointF ForwardVec()
    {
        PointF temp = new PointF();
        temp.X = _pointX - _ForwardPoint.X;
        temp.Y = _pointY - _ForwardPoint.Y;
        return Normalize(temp);
    }
    public PointF Normalize(PointF p)
    {
        PointF temp = new PointF();
        if (p.X > p.Y)
        {
            temp.X = 1;
            temp.Y = p.Y / p.X;
        }
        else if (p.Y > p.X)
        {
            temp.Y = 1;
            temp.X = p.X / p.Y;
        }
        else
        {
            return new PointF(1, 1);
        }
        return temp;
    }
    public void MoveForward()
    {
        _pointX += ForwardVec().X;
        _pointY += ForwardVec().Y;
    }
    public void MoveBackwards()
    {
        _pointX -= ForwardVec().X;
        _pointY -= ForwardVec().Y;
    }
    public void TurnLeft()
    {
        rotAng += 10.0f;
    }
    public void TurnRight()
    {
        rotAng -= 10.0f;
    }
}

Currently when running a program implementing this class, instantiate a default SwarmCir() and just calling the moving functions at the bottom I get really weird results. Essentially I want ‘W’ to move the circle along the “forward vector” which ever way the line is pointing. Obviously for ‘S’ is just reverse. Then when turning I would like the shape and line to turn properly. If more information is needed Please ask. Working towards a complete AI workbench.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:47:33+00:00Added an answer on June 2, 2026 at 2:47 am

    Since you’re only drawing a circle and a line, I would skip the transformations used in the Rotate method. I would also store the forward vector instead of the forward point and rotAngle in the class. And use a PointF for position instead of float _pointX/_pointY, and a radius instead of width/height, since they will be equal for circles. I would also make the fields private to avoid modification from outside the class.

    class SwarmCir
    {
      private PointF _point;
      private PointF _forwardVector = new PointF(1, 0);
      private float _radius = 2.5f;
      private float _vectorLength = 5.0f;
      private Pen _pen = new Pen(Color.Black);
    

    Then, I would use _point as the center point for the circles (in your code, the _pointX/_pointY refers to a point to the top/left of the circle). The draw and move methods then becomes:

    public void Draw(Graphics g)
    {
      g.DrawEllipse(_pen, _point.X - _radius, _point.Y - _radius, 2 * _radius, 2 * _radius);
      g.DrawLine(_pen, _point,
          new PointF(_point.X + _forwardVector.X * _vectorLength,
                     _point.Y + _forwardVector.Y * _vectorLength));
    }
    
    public void MoveForward()
    {
      _point.X += _forwardVector.X;
      _point.Y += _forwardVector.Y;
    }
    

    By the way, I often define some extension methods for PointF to ease adding and multiplying PointF’s and scalars.

    static class PointFExtensions
    {
      public static PointF Add(this PointF point, PointF other)
      {
        return new PointF(point.X + other.X, point.Y + other.Y);
      }
    
      public static PointF Add(this PointF point, float value)
      {
        return new PointF(point.X + value, point.Y + value);
      }
    
      public static PointF Multiply(this PointF point, PointF other)
      {
        return new PointF(point.X * other.X, point.Y * other.Y);
      }
    
      public static PointF Multiply(this PointF point, float value)
      {
        return new PointF(point.X * value, point.Y * value);
      }
    }
    

    This makes the draw and move methods code a bit more elegant in my opinion:

    public void Draw(Graphics g)
    {
      g.DrawEllipse(_pen, _point.X - _radius, _point.Y - _radius, 2 * _radius, 2 * _radius);
      g.DrawLine(_pen, _point, _point.Add(_forwardVector.Multiply(_vectorLength)));
    }
    
    public void MoveForward()
    {
      _point = _point.Add(_forwardVector);
    }
    

    Alright, now there’s just the rotation left. Start by adding the Rotate method to PointFExtensions:

    public static PointF Rotate(this PointF point, double angle)
    {
      angle *= Math.PI / 180.0; // Convert from degrees to radians
      return new PointF(
          Convert.ToSingle(point.X * Math.Cos(angle) - point.Y * Math.Sin(angle)),
          Convert.ToSingle(point.X * Math.Sin(angle) + point.Y * Math.Cos(angle)));
    }
    

    The turn methods then becomes:

    public void TurnLeft()
    {
      _forwardVector = _forwardVector.Rotate(10.0f);
    }
    

    Lets hope that works out. I haven’t compiled and tested this code so there might be some typos…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been writhing a rather large document with LaTeX. Now I would like
I've been building a rather complex system and there's come the time now where
My database designs have previously been rather linear, so I'm getting stumped on what
This is essentially very simple I've just been rather verbose about it. My data
I've been working on a rather complicated chart in R. I have a wireframe
I've just been beaten (rather hardly) on the head by some non-trivial warning from
This has been a rather problematic issue on numerous occasions. We have alot of
I've been having this rather frustrating problem, and having failed so many times, I'm
I have been sold on mod_wsgi and apache rather than mod_python. I have all
I've been doing a captcha script lately and was rather successful with it. However

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.