Πόλεμος των σφαιρών (γραμμένο σε C#)

Πόλεμος των σφαιρών (γραμμένο σε C#)

Το πρόγραμμα που ακολουθεί προσομειώνει την κίνηση σφαιρών διαφορετικού μεγέθους υπο κανόνες και θεωρήματα πλάγιας βολής, ελεύθερης πτώσης,βαρήτητας,κρούσης καθώς και εφραρμογή ορισμένων εφφέ όπως διασπάση και συνένωση σφαιρών. Μπορείτε να κάνετε διάφορες τροποποιήσεις στον κώδικα (αφου τον κατεβάσετε στον υπολογιστή σας) και να παράγετε διαφορετικά και αξιοσημείωτα αποτελέσματα κινήσεων,συγκρούσεων,ενώσεων σφαιρών υπο τις δικές σας προϋποθέσεις και προτιμήσεις. Δεν έχετε παρα να περιεργαστείτε,πειραματιστείτε και να τροποποιήσετε τον κώδικα!

//class sfaira

using System;

using System.Collections.Generic;

using System.Text;

namespace sfaires

{

//orismos orthogoniou plaisiou

struct recto

{

public double x, y;

}

//orismos sfairas

class sfaira

{

const double dt=0.05,g_w=9.81;

public double t_x, t_y, rad, pos_x, pos_y; //to rad paizei rolo kai tou barous

public static recto rect = new recto();

//arxikopoiisi

public sfaira(double t_x,double t_y,double rad, double pos_x,double pos_y)

{

this.pos_x = pos_x;

this.pos_y = pos_y;

this.t_x = t_x;

this.t_y = t_y;

this.rad = rad;

}

//eksomeiwsi

public void emul_run() {

pos_x = pos_x + t_x * dt;

pos_y = pos_y + t_y * dt;

t_y = t_y – g_w * dt;

//akoubise edafos

if ((pos_y-rad)<0) {

t_y = Math.Abs(t_y) / 1.1; //allagi foras,meiwsi tis kinitikis energeias

pos_y =rad+0;

}

//akoubise orofi

if ((pos_y + rad) > rect.y)

{

t_y = -Math.Abs(t_y) / 1.1; //allagi foras,meiwsi tis kinitikis energeias

pos_y = rect.y-rad;

}

//akoubise aristero plaino toixwma

if ((pos_x – rad) < 0)

{

t_x = Math.Abs(t_x) / 1.1; //allagi foras,meiwsi tis kinitikis energeias

pos_x = rad+0;

}

//akoubise deksi plaino toixwma

if ((pos_x + rad) > rect.x)

{

t_x = -Math.Abs(t_x) / 1.1; //allagi foras,meiwsi tis kinitikis energeias

pos_x = rect.x- rad;

}

}

//afti i sinartisei prepei na kaleitai mono kata thn sigrousi 2 sfairwn

//allazei tis taxitites twn sfairwn simfwna me tin elastiki krousi

public void new_speed(ref sfaira sf) {

// u2_n=(m1*(2*u1-u2)+m2*u2)/(m2+m1)

// u1_n=(m2*(2*u2-u1)+m1*u1)/(m2+m1)

double ux1, uy1, ux2, uy2;

ux1 = this.t_x; uy1 = this.t_y;

ux2 = sf.t_x; uy2 = sf.t_y;

sf.t_x = (this.rad * (2 * ux1 – ux2) + sf.rad * ux2) / (sf.rad + this.rad);

sf.t_y = (this.rad * (2 * uy1 – uy2) + sf.rad * uy2) / (sf.rad + this.rad);

this.t_x = (sf.rad * (2 *ux2 – ux1) + this.rad * ux1) / (sf.rad + this.rad);

this.t_y = (sf.rad * (2 * uy2 -uy1) + this.rad * uy1) / (sf.rad + this.rad);

}

//elegxos an egine krousi

public bool check_krousi(sfaira sf)

{

double press= (this.rad + sf.rad)-Math.Sqrt( Math.Pow((this.pos_x – sf.pos_x), 2) + Math.Pow((this.pos_y – sf.pos_y), 2));

//iparxei krousi

if (press>=0) {

new_speed(ref sf);

return true;

}

return false;

}

}

}

//class collection-sfairwn

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;

using System.Drawing;

namespace sfaires

{

class colect_sfairs

{

recto rect;

public List<sfaira> sfairs = new List<sfaira>();

List<int> hits = new List<int>();

//arxikopoiisi

public colect_sfairs(int count_sf,double min_rad, double max_rad,double max_sp_x,double max_sp_y, recto rect,PictureBox pic)

{

sfaira.rect=rect; //geniko plaisio gia oles tis sfaires

sfaira t_sf;

int mx_flex = 10000; //gia tin dimiourgia dekadikwn tixaiwn arithmwn

//tixees theseis kai taxitites

Random rd=new Random();

for (int a = 0; a < count_sf; a++)

{

t_sf = new sfaira(0,0,0,0,0);

t_sf.rad = rd.Next((int)min_rad * mx_flex, (int)max_rad * mx_flex) / mx_flex;

t_sf.pos_x = rd.Next((int)t_sf.rad, (int)(rect.x – t_sf.rad));

t_sf.pos_y = rd.Next((int)((rect.y – t_sf.rad)/2), (int)(rect.y – t_sf.rad));

t_sf.t_x = max_sp_x – 2 * rd.Next((int)max_sp_x * mx_flex) / mx_flex;

t_sf.t_y = max_sp_y – 2 * rd.Next((int)max_sp_y * mx_flex) / mx_flex;

sfairs.Add(t_sf);

hits.Add(0);

}

//dimiourgia axonwn

make_region_picture(pic, rect);

this.rect.x = rect.x;

this.rect.y = rect.y;

}

//ekinisi (eksomeiwsi)

public void run_me()

{

//kinisi olwn

for (int c = 0; c < sfairs.Count; c++)

{

sfairs[c].emul_run();

}

bool kr;

//elegxos gia krouseis

for (int c = 0; c < sfairs.Count; c++)

{

//effe

if (hits[c]>100) {hits[c]=0; sfairs[c].t_x*=10;}

if (hits[c]>100) { hits[c] = 0; sfairs[c].t_y *= 10; }

for (int o = c + 1; o < sfairs.Count; o++) {

kr=sfairs[c].check_krousi(sfairs[o]);

if (kr == true) { hits[c]++; hits[o]++; }

if (kr && sfairs[c].rad > 2 && sfairs[o].rad > 2 )

{

double vg2 = 0.000001;

//diaspasi twn sfairwn se mikroteres

if ((hits[c] > (vg2 * rect.x / sfairs[c].rad) || hits[o] > (vg2 * rect.x / sfairs[o].rad)))

{

sfairs[c].rad = sfairs[c].rad / 2;

sfairs[c].pos_x += sfairs[c].rad;

sfaira sf = new sfaira(-sfairs[c].t_x, sfairs[c].t_y, sfairs[c].rad, sfairs[c].pos_x – 2 * sfairs[c].rad, sfairs[c].pos_y);

sfairs.Add(sf);

hits.Add(0);

return;

}

}

//sisomatwsi sfairwn katw apo orismenes proipotheseis

int vg = 50;

if (kr && sfairs[c].rad < 4 && sfairs[o].rad < 4 && (sfairs[c].t_x>vg ||sfairs[c].t_y>vg || sfairs[o].t_x>vg || sfairs[o].t_y>vg ))

{//sisomatosi

sfairs[c].rad += sfairs[o].rad;

sfairs[c].t_x /= 5;

sfairs[c].t_y /= 2;

sfairs.RemoveAt(o);

}

}

}

}

Graphics fd ;

//orismos perioxis sxediasis

private void make_region_picture(PictureBox pic,recto rect)

{

float x_min = 0;

float x_max =(float) rect.x;

float y_min = 0;

float y_max = (float)rect.y;

float sx = pic.Width / (x_max – x_min);

float sy = pic.Height / (y_max – y_min);

fd = pic.CreateGraphics();

System.Drawing.Drawing2D.Matrix mt = new System.Drawing.Drawing2D.Matrix(1, 0, 0, -1, 0, pic.Height);

fd.Transform = mt;

fd.ScaleTransform(sx, sy, System.Drawing.Drawing2D.MatrixOrder.Prepend);

fd.TranslateTransform(-x_min, -y_min, System.Drawing.Drawing2D.MatrixOrder.Prepend);

// fd.DrawRectangle(new Pen(Color.Red), 10, 10, (float)rect.x, (float)rect.y);

}

//emfanisi twn grafikwn

public void show()

{

Pen pn = new Pen(Color.Blue);

Pen pn2 = new Pen(Color.Gold);

fd.Clear(Color.GhostWhite);

foreach (sfaira sf in sfairs)

{

fd.DrawEllipse(pn, (float)(sf.pos_x – sf.rad), (float)(sf.pos_y – sf.rad), (float)(sf.rad*2), (float)(sf.rad*2));

}

}

}

}

// form

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace sfaires

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

colect_sfairs c_sfair;

//afkisis twn taxititwn twn sfairwn ston katakorifo aksona

private void button2_Click(object sender, EventArgs e)

{

foreach (sfaira sf in c_sfair.sfairs)

{

sf.t_y = Math.Abs(sf.t_y *2);

}

}

recto rc = new recto();

//arxikopoiisi

private void Form1_Load(object sender, EventArgs e)

{

rc.x = 1000;

rc.y = pic1.Height*rc.x/pic1.Width ;

c_sfair = new colect_sfairs(5, 20, 100, 25, 25, rc, pic1);

}

// eksomeiwsi me xrisi xronometrou

private void timer1_Tick(object sender, EventArgs e)

{

for (int a = 0; a < 100; a++)

{

c_sfair.run_me();

c_sfair.show();

}

timer1.Interval = 100;

}

}

}

Blog Widget by LinkWithin

Post to Twitter Post to Delicious Delicious



χωρίς σχόλια »

χωρίς σχόλια

RSS για τα σχόλια αυτού του άρθρου | TrackBack URL

γράψτε ένα σχόλιο