HEJPBézier Library
August 31st, 2009
Bézier curves are named after a French engineer Pierre Bézier, who used them to design smooth automobile bodies.
Today, them are most commonly used in computer graphics because of their smoothness and aesthetics.
Bézier curves are typically cubic, i.e. described by two anchor points and two control points but can be any degree that is computational possible in the time restraints of a particular application. HEJPBézier is a library for generating the series of points that compose a n-th degree Bézier curve.
Example
import uk.co.hejp.Bezier.*;
import java.awt.Point;
Point[] p1 = null, p2 = null;
void setup(){
/* initialise */
size(360, 200);
strokeWeight(2);
background(255);
smooth();
noFill();
/* generate the curve */
p1 = Bezier.bezierPoints(20, new Point(10,100), new Point(123,0), new Point(236,200), new Point(350,100));
p2 = Bezier.bezierPoints(20, new Point(10,100), new Point(123,200), new Point(236,0), new Point(350,100));
/* draw the curves with circles at each point */
beginShape();
stroke(135,170,222); /* pale blue */
for(int i=0;i<p1.length;i++){
vertex(p1[i].x, p1[i].y);
ellipse(p1[i].x, p1[i].y,10,10);
ellipse(p1[i].x, p1[i].y,4,4);
}
endShape();
beginShape();
stroke(255, 85, 85); /* salmon */
for(int i=0;i<p2.length;i++){
vertex(p2[i].x, p2[i].y);
ellipse(p2[i].x, p2[i].y,10,10);
ellipse(p2[i].x, p2[i].y,4,4);
}
endShape();
/* export the image */
save("bezier-example.png");
}
This example is written in Processing, which is basically Java + a few libraries + a simple quick IDE. Comes in handy for small pieces on code.
Infix Expression Evaluation Java Library
August 18th, 2009
Evaluating a infix expression (e.g. “3+6^9″) is both a complex and frequently occuring problem in programming projects. Here is a library to evaluate infix expression.
Usage
package test;
import hejp.RPN.*;
public class Test {
static public void main(String[] args){
String expression = "tanh(10)";
try {
System.out.print(RPN.process(expression).toString() + "\n"); //prints 0.9999999958776927
} catch (Exception e) {
if (e.getMessage().equalsIgnoreCase("Malformed Expression")) {
System.out.print("Malformed Expression");
}
}
}
}
The RPN class only has one method called process this takes an infix expression as a String and returns the result as a Double or throws an Exception whose message is equal to “Malformed Expression”.
The method ‘process(String infix)’ is static therefore there is no need to make an instance of the class RPN. So process is called like so RPN.process(infix) rather than like RPN r = new RPN(); r.process(infix).