0

**As you can see in c# windows form I'm struggling with this code can draw me an ellipse but i need to draw a circle using two point using the mouse, what i want is not using x2 and y2 instead draw the circle with the first points then the radius the problem is with the Circle class the figure class is right **

Figure

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
       //                           the base class
namespace the_inhertence_app
{
    abstract class Figure
    {
        protected int x, y; // these are the first point of the circle (Center)
        protected Color color;
        protected bool selected;    
        public Figure(int x, int y) 
        {
            this.x = x;
            this.y = y;
        }
        public Figure (int x,int y,Color co)
        {
            this.x = x;
            this.y = y;
            this.color = co;
            selected = false;
        }
        abstract internal void Draw(Graphics g);

        internal void Select(bool value)
        {
            selected = value;
        }
    }

}

Circle class

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace the_inhertence_app
{       // we inherted from figure the first point to draw it and now we need the second point 
    class Circle : Figure 
    {
        // and those are the second point
        int x2, y2; // don't use these 
        int radius;
        public Circle(int x, int y) :
            base(x,y) 
        {
            x2 = y2 = 0;
        }
        public Circle(int x, int y, int r ):
            base(x,y)
        {
            this.radius = r;
        }
        public Circle(int x, int y, int x2 , int y2 ) :  
           base(x, y)
        {
            this.x2 = x2; 
            this.y2 = y2; // don't use x2 and y2 in this code 
            int dx = x2 - x;
            int dy = y2 - y;
            double r = Math.Sqrt(dx * dx + dy * dy);
        }
        internal override void Draw(Graphics g)
        {
            Pen pen;
            if (selected)
                pen = new Pen(color, 3);
            else
                pen = new Pen(color);
            g.DrawEllipse(Pens.Black, x, y, x2, y2);  // this is not correct 
        }

    }

}

Form

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

namespace the_inhertence_app
{
    public partial class Form1 : Form
    {
        List<Figure> thelist;
        Circle C1;
        int x1, y1;
        int x2, y2;

        public Form1()
        {
            InitializeComponent();
            thelist = new List<Figure>();
            //C1 = new Circle(150, 150, 250, 250);
            x1 = y1 = 0;
            //x2 = y2 = 0;

        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            if (C1 != null)
                C1.Draw(g);
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            if(x1==0 && y1==0) // first click mouse 
            {
                x1 = e.X; y1 = e.Y;
            }
            else  // second click mouse 
            {
                // creat a figure 
                C1 = new Circle(x1 , y1, e.X, e.Y);    // i think here we should add the redius to make this a proper circle
                this.Invalidate();
                x1 = y1 = 0;

            }
        }
    }
}
  • A circle is an eclipse where both centers c1 and c2 are are the same location. – jdweng Feb 03 '21 at 14:42
  • 1
    That is not how to draw in WinForms. Read [How do I run a function with PaintEventArgs?](https://stackoverflow.com/questions/55995225/) – Dour High Arch Feb 03 '21 at 16:48

0 Answers0