Skip to content

Instantly share code, notes, and snippets.

@yao2030
Created October 29, 2012 01:32
Show Gist options
  • Save yao2030/3970885 to your computer and use it in GitHub Desktop.
Save yao2030/3970885 to your computer and use it in GitHub Desktop.
Post bar code. U.S. Postal System
public class Post
{
public static void halfHeight(double i)
{
StdDraw.setPenRadius(.01);
StdDraw.line(i, 0, i, 1);
}
public static void fullHeight(double i)
{
StdDraw.setPenRadius(0.01);
StdDraw.line(i, 0, i, 2);
}
public static int getDigits(int N)
{
int count = 0;
while(N != 0)
{
N /= 10;
count++;
}
return count;
}
public static void drawGuard(double i)
{
fullHeight(i);
}
public static void drawBar(int N)
{
drawGuard(-0.2);
double j = 0.0;
int n = N;
for(int i = getDigits(N); i > 0; i--)
{
int d = (int)(n / Math.pow(10, i-1));
StdOut.println(d);
if(d == 0)
{
fullHeight(j);
fullHeight(j+0.2);
halfHeight(j+0.4);
halfHeight(j+0.6);
halfHeight(j+0.8);
j += 1.0;
}
else if(d == 1)
{
halfHeight(j);
halfHeight(j+0.2);
halfHeight(j+0.4);
fullHeight(j+0.6);
fullHeight(j+0.8);
j += 1.0;
}
else if(d == 2)
{
halfHeight(j);
halfHeight(j+0.2);
fullHeight(j+0.4);
halfHeight(j+0.6);
fullHeight(j+0.8);
j += 1.0;
}
else if(d == 3)
{
halfHeight(j);
halfHeight(j+0.2);
fullHeight(j+0.4);
fullHeight(j+0.6);
halfHeight(j+0.8);
j += 1.0;
}
else if(d == 4)
{
halfHeight(j);
fullHeight(j+0.2);
halfHeight(j+0.4);
halfHeight(j+0.6);
fullHeight(j+0.8);
j += 1.0;
}
else if(d == 5)
{
halfHeight(j);
fullHeight(j+0.2);
halfHeight(j+0.4);
fullHeight(j+0.6);
halfHeight(j+0.8);
j += 1.0;
}
else if(d == 6)
{
halfHeight(j);
fullHeight(j+0.2);
fullHeight(j+0.4);
halfHeight(j+0.6);
halfHeight(j+0.8);
j += 1.0;
}
else if(d == 7)
{
fullHeight(j);
halfHeight(j+0.2);
halfHeight(j+0.4);
halfHeight(j+0.6);
fullHeight(j+0.8);
j += 1.0;
}
else if(d == 8)
{
fullHeight(j);
halfHeight(j+0.2);
halfHeight(j+0.4);
fullHeight(j+0.6);
halfHeight(j+0.8);
j += 1.0;
}
else if(d == 9)
{
fullHeight(j);
halfHeight(j+0.2);
fullHeight(j+0.4);
halfHeight(j+0.6);
halfHeight(j+0.8);
j += 1.0;
}
n = (int)(n % (Math.pow(10, i-1)));
}
fullHeight(j);
}
public static void setScale(int N)
{
int d = getDigits(N);
StdDraw.setYscale(-2, 2);
StdDraw.setXscale(-0.3, d);
}
public static void main(String[] args)
{
int N = Integer.parseInt(args[0]);
setScale(N);
drawBar(N);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment