• 用Processing生成屏保(二)


    代码

       1: class TPoint
       2: {
       3:     public TPoint(int _x, int _y) {
       4:         super();
       5:         this._x = _x;
       6:         this._y = _y;
       7:     }
       8:     public int _x;
       9:     public int _y;
      10:     TPoint _next;
      11: }
      12:  
      13: List<TPoint> points;
      14: int x;
      15: int y;
      16: final int speed = 20;
      17: final int radius = 5;
      18: int points_num = 20;
      19: int xspeed;
      20: int yspeed;
      21: double angle;
      22: TPoint startPoint;
      23: TPoint endPoint;
      24:  
      25: void calcAngle()
      26: {
      27:     if (endPoint._y == startPoint._y >>
      28:             endPoint._x > startPoint._x)
      29:     {
      30:         angle = HALF_PI;
      31:     }
      32:     else if (endPoint._y == startPoint._y >>
      33:             endPoint._x < startPoint._x)
      34:     {
      35:         angle = PI + HALF_PI;
      36:     }
      37:     else
      38:     {
      39:         angle =  atan((float) ((double)(endPoint._x - startPoint._x)/(double)(endPoint._y - startPoint._y)));
      40:     }
      41: }
      42:  
      43: void calcSpeed()
      44: {
      45:     if (endPoint._x >= startPoint._x)
      46:     {
      47:         xspeed = (int) (speed * abs(sin((float) angle)));
      48:     }
      49:     else
      50:     {
      51:         xspeed = -(int) (speed * abs(sin((float) angle)));
      52:     }
      53:  
      54:     if (endPoint._y >= startPoint._y)
      55:     {
      56:         yspeed = (int) (speed * abs(cos((float) angle)));
      57:     }
      58:     else
      59:     {
      60:         yspeed = -(int) (speed * abs(cos((float) angle)));
      61:     }
      62: }
      63:  
      64: void setupTheme(int theme)
      65: {
      66:     switch(theme)
      67:     {
      68:     case 0:    // random
      69:         {
      70:               points = new ArrayList<TPoint>(points_num);
      71:  
      72:               for (int i=0;i<points_num;i++)
      73:               {
      74:                   points.add(new TPoint((int)random(100, displayWidth - 100), 
      75:                  (int)(random(100, displayHeight - 100))));
      76:               }
      77:  
      78:               for (int i=0;i<points_num;i++)
      79:               {
      80:                   points.get(i)._next = points.get((i+1) % points_num);
      81:               }
      82:  
      83:               startPoint = points.get(0);
      84:               endPoint = points.get(1);
      85:  
      86:               x = startPoint._x;
      87:               y = startPoint._y;
      88:  
      89:               calcAngle();
      90:               calcSpeed();
      91:         }
      92:         break;
      93:     case 1: // stars
      94:         {
      95:              List<TPoint> stars = new ArrayList<TPoint>(5);
      96:               stars.add(new TPoint(452, 196));
      97:               stars.add(new TPoint(867, 189));
      98:               stars.add(new TPoint(526, 494));
      99:               stars.add(new TPoint(669, 67));
     100:               stars.add(new TPoint(822, 472));
     101:  
     102:               points_num = 5;
     103:               points = stars;
     104:  
     105:               for (int i=0;i<points_num;i++)
     106:               {
     107:                   points.get(i)._next = points.get((i+1) % points_num);
     108:               }
     109:  
     110:               startPoint = points.get(0);
     111:               endPoint = points.get(1);
     112:  
     113:               x = startPoint._x;
     114:               y = startPoint._y;
     115:  
     116:               calcAngle();
     117:               calcSpeed();
     118:         }
     119:         break;
     120:     case 2:
     121:         {
     122:             List<TPoint> pulse = new ArrayList<TPoint>(points_num + 2);
     123:  
     124:             pulse.add(new TPoint(100, displayHeight / 2));
     125:             for (int i=0;i<points_num;i++)
     126:             {
     127:                 pulse.add(new TPoint(100 + (i * (displayWidth - 200) / points_num), 
     128:                  (int) (displayHeight / 2  +  
     129:                  (pow(-1, (i % 2))) * (int)(random(100, displayHeight / 2 - 100)))));
     130:             }
     131:             pulse.add(new TPoint(displayWidth - 100, displayHeight / 2));
     132:  
     133:             points_num += 2;
     134:  
     135:             points = pulse;
     136:  
     137:             for (int i=0;i<points_num;i++)
     138:             {
     139:                 points.get(i)._next = points.get((i+1) % points_num);
     140:             }
     141:  
     142:             startPoint = points.get(0);
     143:             endPoint = points.get(1);
     144:  
     145:             x = startPoint._x;
     146:             y = startPoint._y;
     147:  
     148:             calcAngle();
     149:             calcSpeed();
     150:         }
     151:         break;
     152:     default:
     153:         {
     154:  
     155:         }
     156:         break;
     157:     }
     158: }
     159:  
     160: @Override
     161: public void setup() {
     162:   size(displayWidth, displayHeight);
     163:   background(0);
     164:   frameRate(90);
     165:  
     166:   setupTheme(2);
     167: }
     168:  
     169: void determineNextPos()
     170: {
     171:     if (endPoint._y == y >> abs(endPoint._x - x) < abs(speed))
     172:     {
     173:         startPoint = endPoint;
     174:         endPoint = endPoint._next;
     175:  
     176:         calcAngle();
     177:         calcSpeed();
     178:  
     179:         x = startPoint._x;
     180:         y = startPoint._y;
     181:     }
     182:     else if (endPoint._x == x >> abs(endPoint._y - y) < abs(speed))
     183:     {
     184:         startPoint = endPoint;
     185:         endPoint = endPoint._next;
     186:  
     187:         calcAngle();
     188:         calcSpeed();
     189:  
     190:         x = startPoint._x;
     191:         y = startPoint._y;
     192:     }
     193:     else if ((endPoint._x != x) >>
     194:             (endPoint._y != y) >>
     195:             (abs(endPoint._x - x) < abs(speed * sin((float) angle) / 2) ||
     196:             abs(endPoint._y - y) < abs(speed * cos((float) angle) /2 )))
     197:     {
     198:         startPoint = endPoint;
     199:         endPoint = endPoint._next;
     200:  
     201:         calcAngle();
     202:         calcSpeed();
     203:  
     204:         x = startPoint._x;
     205:         y = startPoint._y;
     206:     }
     207:     else
     208:     {
     209:         x += xspeed;
     210:         y += yspeed;
     211:     }
     212: }
     213:  
     214: @Override
     215: public void draw() {
     216:  
     217:  determineNextPos();
     218:  
     219:   colorMode(RGB, 255);
     220:   fill(0,0,0,10);
     221:   rect(-1, -1, displayWidth+1, displayHeight+1);
     222:  
     223:   noFill();
     224:   colorMode(HSB, 255);
     225:   stroke(random(0, 255), 255, 255);
     226:  
     227:   ellipse(x, y, radius, radius);
     228:   ellipse(x, y, radius + 1, radius + 1);
     229:  
     230: }

    截图

  • 相关阅读:
    Windows Phone 独立存储资源管理器工具
    Windows Phone 选择器
    Windows Phone 启动器
    Windows Phone 8 ControlTiltEffect
    ActivatedEventArgs.IsApplicationInstancePreserved 属性
    HttpWebRequest BeginGetResponse EndGetResponse
    python并发编程-进程间通信-Queue队列使用-生产者消费者模型-线程理论-创建及对象属性方法-线程互斥锁-守护线程-02
    python并发编程-进程理论-进程方法-守护进程-互斥锁-01
    python网络编程-socket套接字通信循环-粘包问题-struct模块-02
    python网络编程-异常处理-异常捕获-抛出异常-断言-自定义异常-UDP通信-socketserver模块应用-03
  • 原文地址:https://www.cnblogs.com/long123king/p/3415600.html
Copyright © 2020-2023  润新知