Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I trying to draw a circle with mouse up, move, and down events. I have a circle class. Everything is going fine until I try to draw a circle and it is going out of bounds of my form. Everything is fine when my circle in the left and the upper broader(x=0, y=somepoint) and the upperborder(x=somepoint, y=0). The problem is with the border(width) and with the bottom border(height). I think something is wrong with my calculation.

Can anyone help? Here the mousemove event:
C#
int x = x_pos_checker(e.X);
int y = y_pos_checker(e.Y);
//switch places
m_current_shape.Xpos = (x < m_startpoint.X) ? x : m_startpoint.X;
m_current_shape.Ypos = (y < m_startpoint.Y) ? y : m_startpoint.Y;
double deltax = Math.Pow(x - m_startpoint.X, 2);
double deltay = Math.Pow(y - m_startpoint.Y, 2);
((Circle) m_current_shape).Diam = (float) Math.Sqrt(deltax + deltay);
Invalidate(); //re-paint


Here is the x and y checkers:
C#
private int x_pos_checker(int i_mousex) {
    if (i_mousex < 0) {
        return 0;
    } else if (i_mousex > ClientRectangle.Width) {
        return ClientRectangle.Width;
    } else {
        return i_mousex;
    }
}
private int y_pos_checker(int i_mousey) {
    if (i_mousey < groupBox1.Height) {
        return groupBox1.Height;
    }
    if (i_mousey > ClientRectangle.Height) {
        return ClientRectangle.Height;
    } else {
        return i_mousey;
    }


And the drawmethod in class Circle:

C#
public override void draw(PaintEventArgs e) {
    Graphics Cirlcegraphic = e.Graphics;
    m_currentrectangle = new RectangleF(Xpos, Ypos, (float) m_diam, (float) m_diam); //for hittest
    Cirlcegraphic.FillEllipse(Brushes.White, m_currentrectangle);
}
Posted
Comments
gggustafson 24-Feb-14 2:21am    
Your Xpos and Ypos must be positioned greater than m_diam / 2. Otherwise the circle will display outside the bounds of your form.

HTH

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900