重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.text.DecimalFormat;
10余年建站经验, 成都网站建设、成都网站制作客户的见证与正确选择。创新互联公司提供完善的营销型网页建站明细报价表。后期开发更加便捷高效,我们致力于追求更美、更快、更规范。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class WalkDistance {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for (int i = 0; i n; i++) {
String order = in.next();
System.out.println(new DecimalFormat("0.00").format(calDistance(order)));
}
in.close();
}
private static float calDistance(String order) {
char[] orderChar = order.toCharArray();
ListString orderList = new ArrayListString();
int num = 0;
for (int i = 0, len = orderChar.length; i len; i++) {
if (orderChar[i] = '0' orderChar[i] = '9') {
num++;
} else {
if (num == 0) {
orderList.add(String.valueOf(orderChar[i]));
} else {
StringBuffer sb = new StringBuffer();
for (int j = num; j 0; j--) {
sb.append(String.valueOf(orderChar[i - j]));
}
orderList.add(sb.toString());
orderList.add(String.valueOf(orderChar[i]));
num = 0;
}
}
if (i == len - 1 num != 0) {
StringBuffer sb = new StringBuffer();
for (int j = num - 1; j = 0; j--) {
sb.append(String.valueOf(orderChar[i - j]));
}
orderList.add(sb.toString());
}
}
Point curPoint = new Point(0, 0, 90);
for (int i = 0, len = orderList.size(); i len; i++) {
if ("R".equals(orderList.get(i))) {
curPoint.angle = (curPoint.angle - 90 + 360) % 360;
} else if ("L".equals(orderList.get(i))) {
curPoint.angle = (curPoint.angle + 90) % 360;
} else {
curPoint.setLocation(curPoint, orderList.get(i));
}
}
return curPoint.getDis();
}
static class Point {
private int x;
private int y;
private int angle;
private float dis;
public Point(int x, int y, int angle) {
this.x = x;
this.y = y;
this.angle = angle;
this.dis = (float) 0;
}
public void setLocation(Point point, String distance) {
if (point.angle == 0) {
point.x += Integer.valueOf(distance);
} else if (point.angle == 90) {
point.y += Integer.valueOf(distance);
} else if (point.angle == 180) {
point.x -= Integer.valueOf(distance);
} else {
point.y -= Integer.valueOf(distance);
}
}
public float getDis() {
return (float) Math.sqrt(this.x * this.x + this.y * this.y);
}
}
}
用极坐标方程,一个点一个点的画。给你段我以前写的程序,虽然不是螺旋曲线,但也差不多。import java.awt.*;
import javax.swing.*;public class Spirograph extends JApplet {
public static void main(String s[]) {
JFrame frame = new JFrame();
frame.setTitle("Spirograph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new Spirograph();
applet.init();
frame.getContentPane().add(applet);
frame.pack();
frame.setVisible(true);
}
public void init() {
JPanel panel = new SpiroPanel();
getContentPane().add(panel);
}
}class SpiroPanel extends JPanel{
int nPoints = 1000;
double r1 = 60;
double r2 = 50;
double p = 70;
public SpiroPanel() {
setPreferredSize(new Dimension(400, 400));
setBackground(Color.white);
} public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.translate(200,200);
int x1=(int)(r1+r2-p);
int y1=0;
int x2;
int y2;
for (int i=0; inPoints; i++) {
double t = i*Math.PI/90;
x2 = (int)((r1+r2)*Math.cos(t)-p*Math.cos((r1+r2)*t/r2));
y2 = (int)((r1+r2)*Math.sin(t)-p*Math.sin((r1+r2)*t/r2));
g2.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
}
}
代码如下,只是时间仓促有些简陋,没有坐标轴,而且大小比例问题也没有调好。不过功能实现了。嘎嘎,新手上路,腾云驾雾。
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Bbso extends JPanel{
int x,y,x1,y1,m=100;
double d;
public Bbso() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100,100,500,300);
f.setTitle("画曲线");
f.setVisible(true);
f.getContentPane().add(this);
}
public static void main(String arg[]) {
new Bbso();
}
public void paint(Graphics g) {
super.paintComponent(g);
x1=0;
y1=0;
for(x=-250;x250;x++) {
d=-0.2045*x*x+100.41*x-6736.8; //这里填写公式
y=(int)d;
g.drawLine(x1,y1+m,x,y+m);
x1=x;
y1=y;
}
}
}