/// <summary> /// Determines and sets the center of gravity for a planet. /// If no center of gravity is in range, the planet will not move. /// </summary> /// <param name="possibleCOG"></param> public void DetermineCOG(CenterOfGravity possibleCOG) { var d = Math.Sqrt(Math.Pow(m_location.X - possibleCOG.Location.X, 2) + Math.Pow(m_location.Y - possibleCOG.Location.Y, 2)); var dist = Math.Abs(d); if (dist <= possibleCOG.Radius) { if (dist < m_distanceFromCOG) { m_distanceFromCOG = dist; m_centerOfGravity = possibleCOG; } } }
private void pictureBox1_Click(object sender, EventArgs e) { MouseEventArgs click = e as MouseEventArgs; Point newPoint = new Point(click.X, click.Y); if (centerOfGravityButton.Checked) { int radius = (int)numericUpDown1.Value; //Add a new CenterOfGravity to the list CenterOfGravity newCOG = new CenterOfGravity(newPoint, radius); COG.Add(newCOG); Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height); Graphics circle = Graphics.FromImage(bmp); int index = 0; //display all centers of gravity foreach (CenterOfGravity cog in COG) { int diameter = cog.Radius * 2; circle.FillEllipse(grayBrush, cog.Location.X - cog.Radius, cog.Location.Y - cog.Radius, diameter, diameter); circle.FillEllipse(blackBrush, cog.Location.X - 3, cog.Location.Y - 3, 6, 6); index++; } //display any planets foreach (Planet planet in Planets) { circle.FillEllipse(redBrush, planet.Location.X - 5, planet.Location.Y - 5, 10, 10); } pictureBox1.Image = bmp; } else if (planetButton.Checked) { bool inPull = false; CenterOfGravity gravity = new CenterOfGravity(); double closestDist = 100000.00; //only add the point if it is within the area of the center of gravity //Determine the distance between the point of gravity and the planet foreach (CenterOfGravity cog in COG) { var d = Math.Sqrt(Math.Pow(newPoint.X - cog.Location.X, 2) + Math.Pow(newPoint.Y - cog.Location.Y, 2)); var dist = Math.Abs(d); if (dist <= cog.Radius) { inPull = true; if (dist < closestDist) { closestDist = dist; gravity.Location = cog.Location; gravity.Radius = cog.Radius; } break; } else { inPull = false; } } if (inPull == true) { //add the planet to our list Planet newPlanet = new Planet(newPoint); newPlanet.DetermineCOG(gravity); Planets.Add(newPlanet); Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height); Graphics planet = Graphics.FromImage(bmp); //first redraw any centers of gravity int index = 0; foreach (CenterOfGravity cog in COG) { int diameter = cog.Radius * 2; planet.FillEllipse(grayBrush, cog.Location.X - cog.Radius, cog.Location.Y - cog.Radius, diameter, diameter); planet.FillEllipse(blackBrush, cog.Location.X - 3, cog.Location.Y - 3, 6, 6); index++; } //then draw planets on top. foreach (Planet pt in Planets) { planet.FillEllipse(redBrush, pt.Location.X - 5, pt.Location.Y - 5, 10, 10); } pictureBox1.Image = bmp; } else { MessageBox.Show("Please place the planet in a center of gravity"); } } else // neither is checked and that's an issue { MessageBox.Show("Please select an option: create a center of gravity or a planet."); } }
public void OrbitTester() { CenterOfGravity obj = new CenterOfGravity(); Assert.AreNotEqual(obj, null); }