马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
先来个processing版吧
下面是代码
- int centerX;
- int centerY;
- int state = 0;
- PVector v1;
- PVector v2;
- void setup(){
- size(800,800);
- centerX = width/2;
- centerY = height/2;
- }
- void draw(){
- background(255);
- rectMode(CENTER);
- rect(centerX,centerY,500,500);
- if(state == 0){
- line(centerX,centerY,mouseX,mouseY);
- v1 = new PVector(mouseX-centerX,mouseY-centerY);
- v1.setMag(5);
- v2 = new PVector(centerX,centerX);
- }else{
- reflect();
- v2.add(v1);
- ellipse(v2.x,v2.y,50,50);
- }
- }
- void mouseClicked(){
- if(state == 0){
- state = 1;
- }else{
- state = 0;
- }
- }
- void reflect(){
- if(v2.x>=650||v2.x<=150){
- v1.set(v1.x*-1,v1.y);
- }
- if(v2.y>=650||v2.y<=150){
- v1.set(v1.x,v1.y*-1);
- }
- }
复制代码 接下来是Pyhon版
代码如下(Python代码传不上来,只能截图了)
接下来是C#版
- private void RunScript(bool reset, object y, ref object rect, ref object pt1)
- {
- //Runner run;
- //Point3d pt;
- Rectangle3d rect1 = new Rectangle3d(Plane.WorldXY, rectX, rectY);
- if(reset){
- Random r = new Random();
- Vector3d p = new Vector3d(r.Next(1, rectX - 1), r.Next(1, rectY - 1), 0);
- Vector3d v = new Vector3d(r.Next(1, 20), r.Next(1, 20), 0);
- run = new Runner(p, v);
- }else{
- pt1 = run.position(); //点的当前位置
- Print("{0}", rectX);
- run.run();
- }
- rect = rect1;
- }
- // <Custom additional code>
- int rectX = 500;
- int rectY = 500;
- Runner run;
- public class Runner{
- Vector3d p;
- Vector3d v;
- public Runner(Vector3d p, Vector3d v){
- this.p = p;
- this.v = v;
- }
- public Point3d position(){
- Point3d pt = new Point3d(this.p.X, this.p.Y, 0);
- return pt;
- }
- void reflect(){
- if(this.p.X >= 500 || this.p.X <= 0){
- this.v.X *= -1;
- }
- if(this.p.Y >= 500 || this.p.Y <= 0){
- this.v.Y *= -1;
- }
- }
- public void run(){
- this.reflect();
- this.p = Vector3d.Add(this.p, this.v);
- }
- }
复制代码 额。。还没完,精神分裂还在继续,下面这个是用Python深入下去的。。。模拟蜂群。。先去吃个药。
|
|