Java program – Visualizing electric potential

by Nideesh C on April 29, 2011 · 0 comments

in Java Programming




import java.awt.Color;

public class Potential {

    public static void main(String[] args) {

        // read in N point charges
        int N = StdIn.readInt();
        Charge[] a = new Charge[N];
        for (int k = 0; k < N; k++) {
            double x0 = StdIn.readDouble();
            double y0 = StdIn.readDouble();
            double q0 = StdIn.readDouble();
            a[k] = new Charge(x0, y0, q0);
        }

        // compute the potential at each point and plot
        int SIZE = 800;
        Picture pic = new Picture(SIZE, SIZE);
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                double V = 0.0;
                for (int k = 0; k < N; k++) {
                    double x = 1.0 * i / SIZE;
                    double y = 1.0 * j / SIZE;
                    V += a[k].potentialAt(x, y);
                }
                V = 128 + V / 2.0e10;
                int t = 0;
                if      (V <   0) t = 0;
                else if (V > 255) t = 255;
                else              t = (int) V;
                // Color c = new Color(t, t, t);
                Color c = Color.getHSBColor(t / 255.0f, .9f, .9f);
                // Color c = Color.getHSBColor(.75f*t/40, .8f, .8f);
                //int gray = (t * 37) % 255;
                //Color c = new Color(gray, gray, gray);
                pic.set(i, SIZE-1-j, c);
            }
        }
        pic.show();
    }
}


/*************************************************************************
 *  Execution:    java Potential < input.txt
 *  Dependencies: Charge.java Picture.java StdIn.java
 *
 *
 *  Potential value visualization for a set of charges.
 *
 *  % java Potential < charges.txt
 *  Content of charges.txt  
9
.51 .63 -100
.50 .50   40
.50 .72   10
.33 .33    5
.20 .20  -10
.70 .70   10
.82 .72   20
.85 .23   30
.90 .12  -50

*************************************************************************/



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. Java program – Converting color to grayscale
  2. Java program – Fade effect
  3. Java program – Luminance library
  4. Java program – Array I/O library
  5. Java program – Image scaling

Leave a Comment

Previous post:

Next post: