private double gravity(obj a, obj b) { G = 10000; //double G = 6.67*(0.00000000001); //6,67*10^(-11) double x = a.pos.X - b.pos.X; double y = a.pos.Y - b.pos.Y; double distance = Math.Sqrt(x * x + y * y); //double modifier = 1.5; return(G * a.mass * b.mass / (distance * distance)); }
private void button5_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { using (BinaryReader reader = new BinaryReader(File.Open(openFileDialog1.FileName, FileMode.Open))) { int count = reader.ReadInt32(); objects.Clear(); objects = new List <obj>(); for (int i = 0; i < count; i++) { int size = reader.ReadInt32(); byte[] readen = reader.ReadBytes(size); obj temp = ByteArrayToObject <obj>(readen); objects.Add(temp); } drawObjects(); } } }
private void drawObjects() { if (!pictureBox1.Focused) { pictureBox1.Focus(); } gr.Clear(Color.White); Pen pen = new Pen(Color.Black, 1); if (checkBox5.Checked) { int step = trackBar2.Value; for (int i = 0; i < pictureBox1.Width; i += step) { gr.DrawLine(pen, i, 0, i, pictureBox1.Height); } for (int k = 0; k < pictureBox1.Height; k += step) { gr.DrawLine(pen, 0, k, pictureBox1.Width, k); } } gr.DrawString("SCALE=" + (int)(scale * 100) + "%", new Font(Font, new FontStyle()), new HatchBrush(HatchStyle.Shingle, Color.Black), 0, 0); if (checkBox3.Checked) { int step = trackBar2.Value; for (int i = 0; i < pictureBox1.Height; i += step) { for (int k = 0; k < pictureBox1.Width; k += step) { field temp = null; obj a = new obj(new vector(k, i), new vector(0, 0), 1, true); foreach (var o in objects) { double currForce = gravity(a, o); vector gravVector = new vector(a.pos.X - o.pos.X, a.pos.Y - o.pos.Y); double angleX = gravVector.X / gravVector.lenght(); double angleY = gravVector.Y / gravVector.lenght(); a.force.X += currForce * angleX; a.force.Y += currForce * angleY; temp = new field(a.pos, a.force); //Field.Add(temp); } pen = new Pen(Color.Green, 1); if (temp != null) { gr.DrawLine(pen, (int)temp.B.X, (int)temp.B.Y, (int)temp.E.X + (int)temp.B.X, (int)temp.E.Y + (int)temp.B.Y); } } } //foreach (var field in Field) //{ //} } foreach (var o in objects) { try { //gr.DrawEllipse(new Pen(Color.Black, 3), (int)o.pos.X - o.mass / 2, // (int)o.pos.Y - o.mass / 2, o.mass, o.mass); if (!checkBox4.Checked || (o.texture == null)) { gr.DrawEllipse(new Pen(Color.Black, 3), (int)o.pos.X - o.mass / 2, (int)o.pos.Y - o.mass / 2, o.mass, o.mass); } else { gr.DrawImage(o.texture, (int)o.pos.X - o.mass / 2, (int)o.pos.Y - o.mass / 2); } if (checkBox2.Checked) { gr.DrawLine(new Pen(Color.Red, 2), (int)o.pos.X, (int)o.pos.Y, (int)(o.pos.X + o.speed.X), (int)(o.pos.Y + o.speed.Y)); gr.DrawLine(new Pen(Color.Blue, 2), (int)o.pos.X, (int)o.pos.Y, (int)(o.pos.X + o.force.X), (int)(o.pos.Y + o.force.Y)); } if (checkBox1.Checked && o.avalible) { gr.DrawString(o.ToString(), Font, new HatchBrush(HatchStyle.Shingle, Color.Black), (float)o.pos.X + o.mass / 2, (float)o.pos.Y - o.mass / 2); } } catch (Exception) { } } pictureBox1.Invalidate(); pictureBox1.Update(); }
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { pictureBox1.Focus(); drawObjects(); dravCross(e.X, e.Y, 10); if (stage != 0) { //dravCross(e.X, e.Y, 10); try { switch (stage) { case 1: //рисование массы - диаметра объекта { int size = 1 + (int)(Math.Sqrt((e.X - mX) * (e.X - mX) + (e.Y - mY) * (e.Y - mY)) * 2); sizeLast = size; gr.DrawEllipse(new Pen(Color.Black, 3), mX - (size) / 2, mY - (size) / 2, size, size); if (checkBox1.Checked) { gr.DrawString("MASS:" + size, Font, new HatchBrush(HatchStyle.Shingle, Color.Black), (float)mX + size / 2, (float)mY - size / 2); } //gr.DrawEllipse(); //pictureBox1.Invalidate(); //pictureBox1.Update(); break; } case 2: //ресование вектора скорости { gr.DrawEllipse(new Pen(Color.Black, 3), mX - (sizeLast) / 2, mY - (sizeLast) / 2, sizeLast, sizeLast); gr.DrawLine(new Pen(Color.Red, 3), mX, mY, e.X, e.Y); if (checkBox1.Checked) { gr.DrawString( "XY:" + (e.X - mX) + "," + (e.Y - mY) + ",L:" + ((int)((new vector(e.X - mX, e.Y - mY)).lenght())).ToString(), Font, new HatchBrush(HatchStyle.Shingle, Color.Red), e.X, e.Y); } //pictureBox1.Invalidate(); //pictureBox1.Update(); break; } } if (stage == 3) { stage = 0; obj temp; if (!stoped) { temp = new obj(new vector(mX, mY), new vector(e.X - mX, e.Y - mY), sizeLast, stoped); } else { temp = (new obj(new vector(mX, mY), new vector(0, 0), sizeLast, stoped)); } if (checkBox4.Checked) { if (imgs.Count > 0) { int index = t.selectedIndex; temp.textureIndex = index; temp.texture = ResizeImg(imgs[index].image, sizeLast, sizeLast); } else { MessageBox.Show("textures list is empty. copy PNGs to program folder and restart app."); } } objects.Add(temp); drawObjects(); } } catch (Exception) { } } pictureBox1.Invalidate(); pictureBox1.Update(); }