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:
int x = x_pos_checker(e.X);
int y = y_pos_checker(e.Y);
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();
Here is the x and y checkers:
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:
public override void draw(PaintEventArgs e) {
Graphics Cirlcegraphic = e.Graphics;
m_currentrectangle = new RectangleF(Xpos, Ypos, (float) m_diam, (float) m_diam);
Cirlcegraphic.FillEllipse(Brushes.White, m_currentrectangle);
}