/** * Program to draw a parabola into a PDF file. * * Please note that the parabola is drawn sideways on the page, concave-right, so the y and x values have been interchanged in calculations. (i.e., x=y^2) * * Adapted from tutorial found here: http://itextdocs.lowagie.com/examples/com/lowagie/examples/objects/images/RawData.java , written by Bruno Lowagie and Paulo Soares * * Don't do anything important with this code. It's worth about as much as you paid for it (nothing). * * @author tqadmin@top-quark.com */ import java.io.FileOutputStream; import java.io.IOException; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Image; import com.lowagie.text.pdf.PdfWriter; import com.lowagie.text.PageSize; /** * Using raw data to construct an Image object. */ public class ParabolaMaker { /** * Parabola generation * @param args no arguments needed */ public static void main(String[] args) { // step 1: creation of a document-object Document document = new Document(PageSize.LETTER,0,0,0,0); try { // step 2: // we create a writer that listens to the document // and directs a PDF-stream to a file PdfWriter.getInstance(document, new FileOutputStream("parabola.pdf")); // step 3: we open the document document.open(); // step 4: we add content // ALTER THESE TO CHANGE THE DIMENSIONS OF THE PARABOLA double widthInches = 7; // "width" of the mouth of the parabola double depthInches = 4.25; // how deep the parabola is int PPI = 72; // points/inch int width = (int)(widthInches*PPI); // width, in points - y axis int depth = (int)(depthInches*PPI); // depth, in points - x axis byte [] parabola = new byte[width*depth]; // Create a byte of grayscale data per point. A bit wasteful, but Java can only address bytes, // and it'd be a waste of time to downgrade to bit or nibble resolution. // As is, we're looking at ~485 kB for a full 8.5x11 page. double scaling = depth/Math.pow(width/2,2); // the k constant for the graph x = k y^2 double focalDistance = 1/(scaling*4); // parabolic math, where focal distance f = 1/4k int lineThickness = 1; // horizontal thickness of the parabola trace // Building the image for (int y = 0; y < width; y++) { for (int x = 0; x < depth; x++) { // define the parabola's value at this y-value int ySquared = (int)Math.pow(y-width/2, 2); // fill "lineThickness" pixel(s) to each side of the parabola if ( (x > scaling * ySquared - lineThickness) && (x < scaling * ySquared + lineThickness) ) parabola[y * depth + x] = (byte)0x00; // 0x00 -> black trace else parabola[y * depth + x] = (byte)0xFF; // 0xFF -> background is white // indicate the focal distance if ( x == (int)focalDistance ) parabola[y * depth +x] = (byte)0x80; // 0x80 -> line is gray // indicate line of symmetry if (y == width/2) parabola[y * depth +x] = (byte)0x80; // 0x80 -> line is gray } } // parse the raw data into an image, with arguments of width being "depth", length being "width", 1 color channel, 8 bits per channel, from data "parabola" Image para = Image.getInstance(depth, width, 1, 8, parabola); // center the image on the page para.setAbsolutePosition( (document.getPageSize().width()-depth)/2, (document.getPageSize().height()-width)/2 ); // add image to document document.add(para); } catch(DocumentException de) { System.err.println(de.getMessage()); } catch(IOException ioe) { System.err.println(ioe.getMessage()); } // step 5: we close the document document.close(); } }