Jump to content

need help in mid point ellipse algorithm

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
mahmoud

mahmoud

    Newbie

  • Members
  • Pip
  • 4 posts
i coded algorithm drawing an ellipse
using mid point algorithm
but ellipse is not complete
this is the code
please i want to know what is problem?


 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace prof
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
float rx = 90;
float ry = 70;
float xc = 200;
float yc = 200;
float x = 0;
float y = ry;
float m;
float s;
while (x < y)
{
plot(x, y, xc, yc);
m = fellipse(x + 1, y + (1 / 2), rx, ry);
s = fellipse(x + (1 / 2), y - 1, rx, ry);
if (m < 0)
{
x++;
}
else if (m >= 0)
{
x++;
y--;
}
else if (s <= 0)
{
x++;
y--;
}
else if (s > 0)
{
y--;
}
}
}
public float fellipse(float x, float y, float rx, float ry)
{
return (ry * ry) * (x * x) + (rx * rx) * (y * y) - (rx * rx) * (ry * ry);
}
public void plot(float x, float y, float xc, float yc)
{
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Red, 4);
g.DrawRectangle(p, x + xc, y + yc, 1, 1);
g.DrawRectangle(p, -x + xc, y + yc, 1, 1);
g.DrawRectangle(p, -x + xc, -y + yc, 1, 1);
g.DrawRectangle(p, x + xc, -y + yc, 1, 1);
}
}
}


#2
BuckAMayzing

BuckAMayzing

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
The problem is in your while loop.. try putting while(true) and you'll see what I mean. You'll have to terminate the program, but it will illustrate what I'm talking about.

#3
BuckAMayzing

BuckAMayzing

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
Here's a solution for you, but I'm no mathematician, so you'll have to figure out rounding out the edges.

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

namespace prof
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            float rx = 90;
            float ry = 70;
            float xc = 200;
            float yc = 200;
            float x = 0;
            float y = ry;
            float m = 0;
            float s = 0;
            float lastm = 0;
            while (true)
            {
                m = fellipse(x + 1, y + (1 / 2), rx, ry);
                s = fellipse(x + (1 / 2), y - 1, rx, ry);
                if (lastm < s)
                    break;
                plot(x, y, xc, yc);
                if (m < 0)
                {
                    x++;
                }
                else if (m >= 0)
                {
                    x++;
                    y--;
                }
                else if (s <= 0)
                {
                    x++;
                    y--;
                }
                else if (s > 0)
                {
                    y--;
                }

                lastm = m;
                
            }
        }
        public float fellipse(float x, float y, float rx, float ry)
        {
            return (ry * ry) * (x * x) + (rx * rx) * (y * y) - (rx * rx) * (ry * ry);
        }
        public void plot(float x, float y, float xc, float yc)
        {
            Graphics g = this.CreateGraphics();
            Pen p = new Pen(Color.Red, 4);
            g.DrawRectangle(p, x + xc, y + yc, 1, 1);
            g.DrawRectangle(p, -x + xc, y + yc, 1, 1);
            g.DrawRectangle(p, -x + xc, -y + yc, 1, 1);
            g.DrawRectangle(p, x + xc, -y + yc, 1, 1);
        }
    }
}


#4
mahmoud

mahmoud

    Newbie

  • Members
  • Pip
  • 4 posts
thank you for your help and your effort