>>分享数据结构和算法相关的知识和技术 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 19992 个阅读者 刷新本主题
 * 贴子主题:  绕圆算法 回复文章 点赞(0)  收藏  
作者:javathinker    发表时间:2020-03-15 22:07:36     消息  查看  搜索  好友  复制  引用

  JavaME中实现的绕圆算法。是我去年玩手机的时候写的个小东西,随后我会写一些关于这个问题的深入思考。
它基本实现效果为中间一个圆,外面一个圆围着中间的这个圆做等距环绕
代码为:
=================================
package rounds;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class Rect extends MIDlet implements CommandListener{
  private Command quit;
private Command pause;
private BallPoint ball;
private int[] o=new int[2];

public int rans(){
  Random ran=new Random();
  int re=ran.nextInt();
  re=re%255;
  return re;
}

public class BallPoint extends Canvas{
  
  Random ran=new Random();
  int leng=Math.max(getWidth(), getHeight())-10;
  int radius=leng/2;
  int ox=getWidth()/2;
  int oy=getHeight()/2;
  double degree=0;
  
  /*
   * 利用三角函数求确定角度后小圆心相对大圆心的坐标点
   */

  public double[] TFunction(double degree){
   double re[]=new double[2];
   re[0]=Math.abs(Math.cos(degree)*radius);
   re[1]=Math.abs(Math.sqrt(radius*radius-re[0]*re[0]));
   return re;
  }
  
  public double[] GetXY(double degree){
   double re[]={0,0};
  
   if(degree==0||degree==6.28){
    re[0]=radius;
   }else if(degree==1.57){
    re[1]=-radius;
   }else if(degree==3.14){
    re[0]=-radius;
    re[1]=-radius;
   }else if(degree==4.71){
    re[1]=radius;
   }else{
    re=TFunction(degree);
    if(degree>0&°ree<1.57){
     re[1]=-re[1];
    }else if(degree>1.57&°ree<3.14){
     re[0]=-re[0];
     re[1]=-re[1];
    }else if(degree>3.14&°ree<4.71){
     re[0]=-re[0];
    }
   }
   return re;
  }
  
  protected void paint(Graphics graphics) {
  
   graphics.setColor(255, 255, 255);
   graphics.fillRect(0, 0, getWidth(), getHeight());
   graphics.setColor(255, 0, 0);
   graphics.fillArc(ox-15, oy-15, 30, 30, 0, 360);
  
   double re[]=GetXY(degree);
   int x=(int)re[0];
   int y=(int)re[1];
   x=ox+x;
   y=oy+y;
  
   graphics.setColor(0,0,0);
   graphics.fillArc(x-10,y-10,20,20,0,360);
  
  }
}
  class Rounder extends Thread{
  public void run(){}
}

public Rect() {
  ball=new BallPoint();
  
  quit=new Command("退出",Command.EXIT,1);
  ball.addCommand(quit);
  
  pause=new Command("暂停",Command.STOP,2);
  ball.addCommand(pause);
  
  ball.setCommandListener(this);
}
  protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO Auto-generated method stub
}
  protected void pauseApp() {
  // TODO Auto-generated method stub
}
  protected void startApp() throws MIDletStateChangeException {
  Display.getDisplay(this).setCurrent(ball);
  Rounder rounder=new Rounder();
  for(double i=1;i<=628;i++){
   ball.degree=i/100;
   ball.repaint();
   try{
    rounder.sleep(100);
   }catch(Exception e){}
  }
}
public void commandAction(Command command, Displayable arg1) {
  try{
   if(command==quit){
    destroyApp(true);
    notifyDestroyed();
   }
   if(command==pause){
    pauseApp();
   }
  }catch(MIDletStateChangeException me){
   System.out.println(me+" caught.");
  }
}
}

----------------------------
原文链接:https://blog.51cto.com/buguaisp2/172662

程序猿的技术大观园:www.javathinker.net



[这个贴子最后由 flybird 在 2020-03-16 11:03:57 重新编辑]
  Java面向对象编程-->接口
  JavaWeb开发-->开发JavaMail Web应用
  JSP与Hibernate开发-->通过JPA API检索数据
  Java网络编程-->通过JDBC API访问数据库
  精通Spring-->Vue Router路由管理器
  Vue3开发-->Vue组件开发基础
  64匹马,8个赛道,找出跑得最快的4匹马
  有关图片的LZW算法的原理
  算法学习与收集:一些有用的算法网站和网页
  常见的调度算法
  图像基本处理算法的简单实现
  java 通配符的应用范例, java 排序算法
  进程调度算法总结
  Java 选择排序算法
  PageRank算法
  活动安排问题(贪心算法)
  无向图的最短路径求解算法之——Dijkstra算法
  LRU算法的简单实现范例
  Binary Search二分查找算法
  为什么要学数据结构?
  小米面试算法题:搜索旋转排序数组
  更多...
 IPIP: 已设置保密
楼主      
1页 0条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。