Title: Physics Simulation Author: Kevin Stueve Course: Math 480 Programming for the Working Mathematician Instructor: William Stein Quarter: Spring 2010 School: University of Washington Date: Friday, June 4 2010 Overview: A demonstration of graphics and physics programming that shows the results of additive and subtractive color mixing in Java. Before the final program was made in Java, the author implemented communication between Java and Python. This is discussed below. Programming Done for Java/Python communication: The author has an interest in physics and sought to make do physics simulation. Before deciding on making a color mixing program, the author implemented a Java and Python program (Google Code project PyPhysSim revision 29) that communicate over the command-line using the Python subprocess module. The Python program sends drawing commands (a subset of SVG's path element) to the Java program, and the Java program sends mouse commands to the the Python program. The protocol for these messages was designed by the author and are described below: Messages from Python Solver to Java GUI CREATE_SHAPE Parameters: String Id, RGBCOLOR, curve+ RGBCOLOR: redcomponent, greencomponent, bluecomponent (each between 0 and 255 inclusive) curve: cubicTo|lineTo|moveTo|quadTo|closePath cubicTo: "C", float x1, float y1, float x2, float y2, float x3, float y3 http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/GeneralPath.html#curveTo lineTo: "L", float x, float y http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/GeneralPath.html#lineTo moveTo: "M", float x, float y http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/GeneralPath.html#moveTo quadTo: "Q", float x1, float y1, float x2, float y2 http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/GeneralPath.html#quadTo closePath: "Z" http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/GeneralPath.html#closePath Meaning: CREATE_SHAPE implements a subset of SVG Paths (http://www.w3.org/TR/SVG/paths.html) that is included in the GeneralPath class (http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/GeneralPath.html). To make parsing easier, tokens are separated with spaces, with no commas allowed. Only absolute coordinates are allowed, hence only capital letters are allowed. Examples: CREATE_SHAPE awesomeshape 255 0 0 M -1 -1 L -1 1 L 1 1 L 1 -1 Z DELETE_SHAPE Parameters: StringWithoutWhiteSpace Id Meaning: Removes a shape from the simulation. TRANSLATE_SHAPE Parameters: StringWithoutWhiteSpace Id, float tx,float ty, float theta, float anchorx,float anchory Meaning: Rotates shape by an angle of theta radians about the point (anchorx,anchory), followed by a translation of (tx,ty). Examples: TRANSLATE_SHAPE shape1 50 -32 3.14 0 0 Rotates shape1 by pi radians (clockwise) about the origin (0, 0), followed by a translation of (50, -32). REPAINT Parameters: None Meaning: Tells Java to repaint the GUI. Messages from Java GUI to Python Solver See http://java.sun.com/javase/7/docs/api/javax/swing/event/MouseInputListener.html for info on Java mouse events. Each message is a list of strings separated by spaces. MOUSE_CLICKED Parameters: int x, int y, int button, int numclicks Meaning: Mouse button button was clicked at location x, y (relative to the top left corner of the drawing canvas). Examples: MOUSE_CLICKED 30 50 1 2 Mouse button 1 was double clicked at location (30, 50) MOUSE_PRESSED Parameters: int x, int y, int button MOUSE_RELEASED Parameters: int x, int y, int button MOUSE_ENTERED Parameters: int x, int y MOUSE_EXITED Parameters: int x, int y MOUSE_MOVED Parameters: int x, int y MOUSE_DRAGGED Parameters: int x, int y ButtonPressed Parameters: String nameOfButton WINDOW_CLOSED Parameter: None Meaning: The Java window was closed. Physics Background: Light is made up of many photons, each which has a wavelength. The visible part of the spectrum ranges between approximately 400 and 700 nm. The human eye retina has rods and cones to detect light. Cones come in red, blue, and green. Rods are more sensitive, and help with night vision, but don't provide much color information. The fovea is the part of the retina in the middle of the vision field and is the most sensitive and has the most cones. At night, if you want to see a very dim star, it is easier to see it if you look slightly away from it, because there are more rods away from the very center of your field of vision. Cones are sensitive to light in certain frequency ranges corresponding to red, blue, and green. Different mixtures of light will stimulate these cones in different amounts. Several different mixtures can lead to the same color perception-for example computer monitors can produce the sensation of any color in the rainbow with mixtures of red blue and green light; an actual rainbow consist of different mixtures of red, blue, and green, but consists of a continuous spectrum of all frequencies in the visible range. The reason why there are three primary colors is that there are three colors of cones in the human eye. If we had the same vision as other species, and had a fourth cone that can detect ultraviolet light, it would be likely that our computer monitors would also have elements to produce ultraviolet light. Our eyes perceive only the very basic color information. Each type of cone is sensitive to a range of light wavelengths that are really not one color, but an infinite spectrum of colors. Human perceive colors as a point in a three dimensional space, but really they are points in a Hilbert space. Any combination of wavelengths in the visual spectrum constitutes a unique color. Philosophical Tangent (skip if desired): It is often asked what it would be like to perceive another color, or how it would be possible to know if one person's perception of red is the same as another's. Could a robot have the same perception of red as a human? There is a thought experiment to solve this problem. The thought experiment begins with a human subject. Neuron by neuron, the subject's brain is replaced by electronic prosthetics (this thought experiment assumes that the brain can be simulated by a computer). It would be unnatural to believe that at some point the perception of color would change-if one is a materialist then the action of the brain will remain the same during the entire procedure. This leads to the conclusion that whatever experience of color or qualia that humans can perceive can also be perceived by a computer. If one is a strict materialist, then the ego is an illusion and the experience of color is only the firing of certain neurons in the brain, and nothing more. As humans we have a fear of death, and can rest assured that if we had our brain scanned and simulated by a computer, whatever qualia or internal experience of reality we have presently would carry on in the computer. Programming and Math Background: In the Java/Python communication program the shapes are drawn as SVG path elements. Circles and ellipses are made up of four cubic splines. In the color mixing program, many individual points can be created, each with its own color. These points move around, each with its own velocity in the x and y dimensions. If points are sufficiently close together, they will repel each other. This force, damping (which slows motion), and gravity, if turned on are summed, and the resulting change in velocity and position are calculated using Euler's method. Specialized data structures are used to keep track of the positions of the particles. If a “double for loop” were used to find all particles in a list that are close to each other, the time complexity would be quadratic. Instead, map data structures that allow nearby particles to be found are used. As a particle's position changes, this structure must be updated. The performance of this sort of algorithm approaches linear with respect to the number of particles. Color Mixing Algorithm: To determine the color of each pixel, the colors of particles within a small distance are considered. In the subtractive model, amounts of red, blue, and green are taken away from white. In the additive model, amounts of red, blue, and green are added to black. Another option is averaging, which allows red and white to make pink without one color overpowering the other. Conclusion: This project demonstrates color mixing and how human color perception works, along with some physics programming. It would be worthwhile to make finger-paint interaction, where colors can be pushed around with the cursor. Sadly the best way to do this is probably to forgo a physically accurate model. Some future work would be to improve the color mixing to more closely resemble interaction with paint, and allow tablet interaction.