Skip to content

Instantly share code, notes, and snippets.

@miho
Created November 17, 2016 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miho/18ab26e3889495161f9eb61d811e958d to your computer and use it in GitHub Desktop.
Save miho/18ab26e3889495161f9eb61d811e958d to your computer and use it in GitHub Desktop.
@ComponentInfo(name="SimplePlotter", category="ODE")
public class SimplePlotter implements Serializable {
private static final long serialVersionUID = 1L;
transient private BufferedImage image;
@OutputInfo(options="fixedAspectRatio=true")
public BufferedImage plot(
@ParamInfo(name = "Input Trajectory") ArrayList<double[]> t1) {
image = new BufferedImage(640, 640, BufferedImage.TYPE_INT_ARGB);
// init paint device
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(new Color(154, 158, 166));
Composite original = g2.getComposite();
AlphaComposite ac1 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.65f);
g2.setComposite(ac1);
float strokeWidth = 4.0f;
g2.setStroke(new BasicStroke(strokeWidth));
g2.setComposite(original);
// trajectory 1
g2.setColor(Color.GREEN);
double minX = Double.MAX_VALUE;
double minY = Double.MAX_VALUE;
double maxX = Double.MIN_VALUE;
double maxY = Double.MIN_VALUE;
// find min / max values
for (double[] v1 : t1) {
double xValue = v1[1];
double yValue = v1[2];
if (minX > xValue) {
minX = xValue;
}
if (minY > yValue) {
minY = yValue;
}
if (maxX < xValue) {
maxX = xValue;
}
if (maxY < yValue) {
maxY = yValue;
}
}
double dx = Math.abs(maxX - minX);
double dy = Math.abs(maxY - minY);
double xScale = (image.getWidth() -strokeWidth*2 )/ dx;
double yScale = (image.getHeight() -strokeWidth*2 )/ dy;
double xOffset = (maxX + minX) / 2;
double yOffset = (maxY + minY) / 2;
int xImageOffset = image.getWidth() / 2 - (int) (xOffset * xScale);
int yImageOffset = image.getHeight() / 2 - (int) (yOffset * yScale);
int x = (int) (t1.get(0)[1] * xScale) + xImageOffset;
int y = (int) (t1.get(0)[2] * yScale) + yImageOffset;
int xTmp = (int) (t1.get(0)[1] * xScale) + xImageOffset;
int yTmp = (int) (t1.get(0)[2] * yScale) + yImageOffset;
// plot the trajectory
for (double[] v1 : t1) {
x = (int) (v1[1] * xScale) + xImageOffset;
y = (int) (v1[2] * yScale) + yImageOffset;
g2.drawLine(xTmp, yTmp, x, y);
xTmp = x;
yTmp = y;
}
g2.dispose();
return image;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment