GitBook 提供支持
2008-11-18 长距离打车如何省钱?
学JavaScript的时候写的一个用于在长距离打车时,计算多远抬一次表最省钱的抬表方案的小程序,参数可以根据需要随意修改。
X轴是距离,Y轴是费用,不同的颜色表示不同的抬表方案
大致的简单结论是20公里以上的时候,每15公里抬一次表,能省出10%左右吧。
下面代码存为taxi.html,然后打开即可在浏览器中看到结果。
1
<html>
2
<script type="text/javascript" src="http://www.walterzorn.com/scripts/wz_jsgraphics.js"></script>
3
4
<body>
5
<script type="text/javascript">
6
<!--
7
function getPrice(distance, night) {
8
var unitPrice = 2.0,
9
basicDistance = 3,
10
basicPrice = 10,
11
extDistance = 15,
12
extPrice = 3.0;
13
14
var price = 0.0;
15
if ( distance == 0 ) return 0;
16
if ( distance <= basicDistance ) return basicPrice;
17
18
if ( distance <= extDistance ) {
19
price = basicPrice + unitPrice * (distance - basicDistance);
20
return price;
21
}
22
23
price += basicPrice + unitPrice*(extDistance-basicDistance) + extPrice*(distance-extDistance)
24
return price;
25
}
26
27
function getPriceWithReset(distance, night, reset) {
28
if ( reset == 0 ) {
29
return getPrice(distance, night);
30
} else {
31
return getPrice(reset, night) * Math.floor(distance/reset) + getPrice(distance % reset, night);
32
}
33
}
34
35
var coordXUnit = 8, coordYUnit = 3,
36
basicX = 100, basicY = 50,
37
coordMaxX = 101, coordMaxY = 251;
38
39
function drawString(str, x, y) {
40
jg.drawString(str, basicX+x*coordXUnit, basicY+(coordMaxY-y)*coordYUnit);
41
}
42
43
function drawPoint(x, y) {
44
drawLine(x, y, x, y);
45
}
46
47
function drawLine(x1, y1, x2, y2) {
48
jg.drawLine(basicX + x1*coordXUnit, basicY + (coordMaxY-y1)*coordYUnit, basicX + x2*coordXUnit, basicY + (coordMaxY-y2)*coordYUnit);
49
}
50
51
function drawPolyline(ax, ay) {
52
var axx = new Array(), ayy = new Array();
53
54
for (var e in ax) {
55
axx.push(basicX + (ax[e] * coordXUnit) );
56
}
57
58
for (var e in ay) {
59
ayy.push(basicY + (coordMaxY - ay[e])*coordYUnit);
60
}
61
62
jg.drawPolyline(axx, ayy);
63
}
64
65
function drawCoordinate() {
66
jg.setColor("#ee8800");
67
drawLine(0, 0, coordMaxX, 0);
68
drawLine(0, 0, 0, coordMaxY);
69
70
drawString(0, -2, -2);
71
for (var i = 1; i < coordMaxX; i++) {
72
if (i%5==0) {
73
// draw grid
74
jg.setStroke(Stroke.DOTTED);
75
drawLine(i, 0, i, coordMaxY);
76
// draw mark
77
jg.setStroke(0);
78
drawLine(i, 0, i, -2);
79
drawString(i, i-1, -3);
80
} else {
81
drawLine(i, 0, i, -1);
82
}
83
}
84
85
for (var i = 1; i < coordMaxY ; i++) {
86
if (i%5==0) {
87
// draw grid
88
jg.setStroke(Stroke.DOTTED);
89
drawLine(0, i, coordMaxX, i);
90
// draw mark
91
jg.setStroke(0);
92
drawLine(0, i, -2, i);
93
drawString(i, -7, i+1);
94
} else {
95
drawLine(0, i, -1, i);
96
}
97
}
98
}
99
100
function drawPriceLine(night, reset) {
101
var ax = new Array(), ay = new Array();
102
103
for (var d=0; d<70; d++) {
104
ax.push(d);
105
ay.push(getPriceWithReset(d, 0, reset));
106
}
107
drawPolyline(ax, ay);
108
}
109
110
/////////////// main section ///////////////
111
var jg = new jsGraphics();
112
jg.setFont("verdana,geneva,sans-serif", "10px", Font.PLAIN);
113
114
drawCoordinate();
115
drawString("reset:", 0, -10);
116
jg.setColor("#000000");
117
drawPriceLine(0, 0);
118
drawString("0", 5, -10);
119
jg.setColor("#FF0000");
120
drawPriceLine(0, 10);
121
drawString("10", 10, -10);
122
jg.setColor("#00FF00");
123
drawPriceLine(0, 15);
124
drawString("15", 15, -10);
125
jg.setColor("#0000FF");
126
drawPriceLine(0, 20);
127
drawString("20", 20, -10);
128
129
///////////////// from wangjing to my home, 35KM /////////////////
130
jg.setColor("#880000");
131
var xp = new Array(), yp = new Array();
132
for ( var i=10; i<30; i++) {
133
xp.push(i);
134
yp.push(getPriceWithReset(35, 0, i));
135
}
136
drawPolyline(xp, yp);
137
138
drawString(getPriceWithReset(35, 0, 15), 0, -20);
139
jg.paint();
140
//-->
141
</script>
142
</body>
143
</html>
Copied!
最近更新 1yr ago
复制链接