3
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        int x1, x2, wid = 100;

        public Form1()
        {
            InitializeComponent();

            x1 = this.Width / 2 ;
            x2 = this.Height / 2 ;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {


            e.Graphics.DrawEllipse(Pens.Red, x1,x2, wid, wid);
        }
    }
}

I want to draw a simple circle in the middle of the form and then later on i want to draw lines coming out from the circle center. How can i do it ?

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
Daniel Lip
  • 3,867
  • 7
  • 58
  • 120
  • *(`x1`, `x2`)* should be the circle center. – obataku Sep 10 '12 at 01:13
  • This appears to be a homework question. Have you tried to solve this problem and, if so, what is your specific problem with the implementation? – JP. Sep 10 '12 at 01:28
  • @oldrinb, you are wrong: If you use (x1, x2), then you will be off-center every time! You should instead construct a rectangle, use the extension methods from here (http://www.codeproject.com/Tips/403031/Extension-methods-for-finding-centers-of-a-rectang) to get the center point of the rectangle, and then use the rectangle to construct the ellipse. – devinbost Sep 01 '14 at 19:58

2 Answers2

5

the properties this.Width and this.Height are the same as this.Bounds which describes itself as:

Gets or sets the size and location of the control including its non-client elements, on pixels, relative to the parent control

this means you'd need to adjust for the thickness of the borders and the title bar. Using this.ClientRectangle avoids that whole issue.

public partial class Form1 : Form
{
    int circleDiameter  = 100;

    public Form1()
    {
        InitializeComponent();
    }

     private void Form1_Paint(object sender, PaintEventArgs e)
    {

        Point CenterPoint = new Point()
        {
            X = this.ClientRectangle.Width/2,
            Y = this.ClientRectangle.Height/2
        };
        Point topLeft = new Point()
        {
            X=(this.ClientRectangle.Width - circleDiameter) / 2,
            Y=(this.ClientRectangle.Height - circleDiameter) / 2
        };
        Point topRight = new Point()
        {
            X=(this.ClientRectangle.Width + circleDiameter) / 2,
            Y=(this.ClientRectangle.Height - circleDiameter) / 2
        };
        Point bottomLeft = new Point()
        {
            X=(this.ClientRectangle.Width - circleDiameter) / 2,
            Y=(this.ClientRectangle.Height + circleDiameter) / 2
        };
        Point bottomRight = new Point()
        {
            X=(this.ClientRectangle.Width + circleDiameter) / 2,
            Y=(this.ClientRectangle.Height + circleDiameter) / 2
        };

         e.Graphics.DrawRectangle(Pens.Red, topLeft.X, topLeft.Y, circleDiameter, circleDiameter);
         e.Graphics.DrawLine(Pens.Red, CenterPoint, topLeft);
         e.Graphics.DrawLine(Pens.Red, CenterPoint, topRight);
         e.Graphics.DrawLine(Pens.Red, CenterPoint, bottomLeft);
         e.Graphics.DrawLine(Pens.Red, CenterPoint, bottomRight);
    }

    private void Form1_Resize(object sender, EventArgs e)
    {
        this.Invalidate();
    }

}
Brad
  • 11,934
  • 4
  • 45
  • 73
  • Brad thats great for the drawing a circle or a square in the middle of the Form. Now i need to find how to calculate and draw a line or a pixle from the circle then when its a square and then when its a rectangle center. – Daniel Lip Sep 10 '12 at 01:22
  • I think there's enough in there for you to play with and see how to draw other lines. to make the lines go from the center of a circle to its edges you'll have to use some trig functions which are in the `System.Math` library. Either use SOH CAH TOA or Pythagorean. either one will get your rays to the edge of the circle – Brad Sep 10 '12 at 01:34
4

If I understand you correctly, is this what you want to do?

        const int circleRadius = 150;
        const int circleDiameter = circleRadius * 2;

        const double angleOfLineInDegrees = 65;
        const double angleOfLineInRadians = (angleOfLineInDegrees / 180) * Math.PI;

        // Center of the form
        var cirleCenter = new PointF(((float)Width / 2), ((float)Height / 2));

        // Ellipses draw like a rectangle. 
        // So just start at the center 
        // subtract by the radius along x and y axis
        // make the width and height the diameter.
        e.Graphics.DrawEllipse(Pens.Black, cirleCenter.X - circleRadius, cirleCenter.Y - circleRadius, circleDiameter, circleDiameter);

        // This line is using trig to convert the angle into a usable vector
        var lineVector = new PointF((float)Math.Cos(angleOfLineInRadians) * circleRadius, (float)Math.Sin(angleOfLineInRadians) * circleRadius);

        var lineEndPoint = new PointF(cirleCenter.X + lineVector.X, cirleCenter.Y + lineVector.Y);

        // Draw the line starting at 
        e.Graphics.DrawLine(Pens.Blue, cirleCenter, lineEndPoint);
yellow_nimbus
  • 335
  • 1
  • 10