Slowing Down is the Key…..

slow down

Slowing down is the key to increased speed.

Past couple of months I was dabbing with fortran GUI and trying pgplot graphics library. I have produced gui’s in c, vb and then integrated them with fortran, but creating GUIs from fortran was new to me.

As the exploration began I took the fire aim adjust approach!! Dived deep into the tutorials and anything that I could lay my hands on.

Quickly from tutorials I graduated to actually creating my own little programs. This went on for a couple of months.

In the beginning I was sprinting as hard as possible. Learning, doing, getting stuck, reading and then doing again. The pace was fast.

But as I become comfortable, my approach shifted. I slowed.

I wrote a program and pondered how and what am I actually doing. This slowing down and pondering doubled my learning. It felt like I was learning at greater pace with this slowdown.

So the technique I want to advocate to anyone learning a new programming language, a new analysis tool or cad software, is to sprint in the first few weeks. Race and learn as much as you are able to handle. Dive deep and continue the pace as long as you are able to.

When exhaustion, sense of acheivement begins to creep in, slow down. Become deliberate in what you do? Question why and what you are doing?

I hope applying this method will help you as much as it has helped me.

What are your views, do let it out in the comments.

Pick-up Sticks

pick-up-sticksAbout a year ago, I was brushing my java programming skills. Mr. K, my colleague from the next cubicle, a seasoned java professional was helping me.

At this very time, my mom came to visit us and she brought this game for my daughter.

So getting bored of creating dummy classes for fictitious banks and its clients to learn java oops programming, I decided to make the game my daughter was playing with physical skicks to something that can be played on the browser.

This is the code that I developed. Not complete game but a decent effort that taught me more than I ever learnt from those dry tutorial examples.

And if you would like to play then, visit this page.

Here’s the full code of the game for anyone to play and improve upon.

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Line2D;


import javax.swing.JApplet;


public class StickFree extends JApplet implements MouseListener {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	Image img;
	Graphics off;
	int w,h,nsticks,stickdetected;
    Stick sticks[];



	public void init(){
		int x1,y1,x2,y2,tmp;
		w=400;
		h=320;
		nsticks=8;
		stickdetected = 0;
		setBackground(Color.black);
        setSize(w,h);
	    img = createImage(w,h);
	    off = img.getGraphics();
        addMouseListener(this);
        sticks = new Stick[nsticks];
		for (int i=0;i<nsticks;i++){  //for (int i=nsticks-1;i>=0;i--){
		  x1 = (int) (Math.random()*w/2-1);
		  y1 = (int) (Math.random()*h-1);
		  x2 = (int) x1+w/2 ;//(Math.random()*w-1);
		  y2 = (int) (Math.random()*h-1);
		  tmp = (int) (Math.random()*5+1);


		  switch(tmp) {
          case 1:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.red, off);break;
          case 2:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.green, off);break;
          case 3:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.white, off);break;
          case 4:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.yellow, off);break;
          default:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.pink, off);break;
          }


         sticks[i].paint();
		}


	}

//	public boolean isontop(int k){
//		boolean ans =true;
//		int i=0;
//
//		for (i=nsticks-1;i>=0;i--){
//			if(i == k) continue;
//			if(sticks[i].isVisible()){
//
//			}
//		}
//
//
//		return ans;
//
//	}

	
	public boolean isontop(Stick sk1){
		int index = sk1.getId();
		
		System.out.println("Index ...from isontop ....."+index);
		
		//boolean ans;
		Stick sk2 = null;
		
		double x ;
		double y ;
		
		for (int i= index; i<nsticks-2;i++){
			sk2 = sticks[i+1];
			
			if(sk2.isVisible()){
				x = (sk2.intercept - sk1.intercept) / (sk1.slope - sk2.slope);
				y = (sk2.slope * x) + sk2.intercept;
				
				
				////
				double a,b,c;

		        a = sk1.slope*x+sk1.intercept-y;
				b = Math.pow(Math.pow(sk1.slope,2.0)+1.0,0.5);

				c = Math.abs(a/b);
//				System.out.println("c = "+c);
				if(c <= 2.0) {
					return false;
				}

//		        System.out.println("cccc"+c+" "+b+" "+a+"slope "+slope+"in "+intercept);
				//return ans;
				////
			}
		}


		return true;

	}

	public void paint(Graphics g){
		 g.drawImage(img, 0, 0, this);
	}

	public void mouseClicked(MouseEvent e) {

	}

	public void mouseEntered(MouseEvent e) {

	}

	public void mouseExited(MouseEvent e) {


	}

	public void mousePressed(MouseEvent e) {


	}

	public void mouseReleased(MouseEvent e) {
        int xx,yy;
		xx=e.getX();
		yy=e.getY();
 	    System.out.println(xx+ " "+ yy+"  n "+stickdetected);

       if(stickdetected >=nsticks) return;

    	   for(int k=0;k<nsticks;k++){
    		   System.out.println(k+"  ....  order ...  ....."+sticks[k].getId());
    		            if(sticks[k].isVisible()){
    		            	
    		            	
    	                 if(sticks[k].isinside(xx,yy)){
    	                		System.out.println("Index ...  ....."+k);
    	                	if(isontop(sticks[k])){
    	                		sticks[k].setVisible(false);
    	                    	   stickdetected++;
    	                    	  break;	
    	                	}
    	                 }
    		          }
    	   }


                       repaint();
                       paintall();
      }

	public void paintall(){
      for(int i=0;i<nsticks;i++){
    	  sticks[i].paint();
      }
	}




}


class Stick {

	private int x0,y0,x1,y1,id,top,down;
	private Color col;
	private Graphics g;
	private boolean visible;
	double slope,intercept;
	Line2D lineShape = null;
	
	public Stick(int id_,int x0_,int y0_,int x1_,int y1_,Color  col_,Graphics g_){
		id = id_;
		x0=x0_;
		y0=y0_;
		x1=x1_;
		y1=y1_;
		col =col_;
		g=g_;
		visible = true;

		slope = ((double)(y1-y0)/(double)(x1-x0));
		intercept = y0-slope*x0;
		top=-1;
		down=-1;
//		System.out.println("id "+id+" x0 "+x0+" x1 "+x1+" y0 "+y0+" y1 "+y1+" slop "+slope+ " in "+ intercept);

		lineShape = new Line2D.Float((float)x0,(float)y0,(float)x1,(float)y1);
	}

	public void paint(){
     if(visible){
    	 
    	
    	 //((Graphics2D)g).
	   g.setColor(col);
	   ((Graphics2D)g).setStroke(new BasicStroke(3));
	   for(int i=0;i<2;i++){
		      g.drawLine(x0+i, y0+i, x1+i, y1+i);
		    }
	    g.drawString(""+this.id, x0, y0);
     }else{
    	 g.setColor(Color.black);

     }



	}

	public void clear(){
		  g.setColor(Color.black);
		    for(int i=0;i<2;i++){
		      g.drawLine(x0+i, y0+i, x1+i, y1+i);
		    }

	}

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.clear();
		this.visible = visible;
	}

	public boolean isinside(int x,int y){
		boolean ans=false;
		double a,b,c;

        a = slope*x+intercept-y;
		b = Math.pow(Math.pow(slope,2.0)+1.0,0.5);

		c = Math.abs(a/b);
//		System.out.println("c = "+c);
		if(c <= 2.0) ans=true;

//        System.out.println("cccc"+c+" "+b+" "+a+"slope "+slope+"in "+intercept);
		return ans;
		
//		double yyyy =  slope * (x0-x)  + y;
//		
//		System.out.println("x0  "+x0+" y0 "+y0+"  & x1 "+x1+" y1 "+y1+"    ------------ x "+x+"  y "+y+"   ???  "+ yyyy);
//return (y0==yyyy);
	}

	public Line2D getLineShape() {
		return lineShape;
	}

	public int getId() {
		return id;
	}

}



And if you would like to play then, visit this page.

I wish WordPress.com allowed embedding java applets. ;(

Monte Carlo Java Calculating the value of pi

MonteCarlo Java

Monte Carlo. Can’t get enough of it,  so here’s the Java version of the program that calculates value of pi with Monte Carlo.

Have done it in Fortran using pgplot as the GUI engine. You can see it action here.

See the Java applet in action

One thing though, the Java program takes more time to converge on the  value of pi than the Fortran. Maybe my rusty Java program.

 

Neutron Collisions, Stanislaw Ulam , Solitares and my Pgplot practice.

 

Java code for calculating the value of pi using Monte Carlo.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MonteCarlo extends JApplet implements Runnable  {

    private static final long serialVersionUID = 1L;

    int width,height,pcnt=1,pin=1;
    double pival,rad=100.0,radi;
    double eps=0.000001,krad;
    double diff;
    int ranx,rany;
    Thread t = null;
    boolean threadSuspended;
    Image offscreenImage;
    Graphics offscreenGraphics;
    int xbar=2,ybar=2,barwid,barheigt=21;
    double bartop =3.04;
    double barbot = 3.21;
    double barmid = 3.14;
    double barval;
    int line;

   public void init(){
       setSize((int)(2*rad),(int) (2*rad+25));

       krad=Math.pow(rad,2.0);
       width= getSize().width;
       height = getSize().height;
       setBackground(Color.black);
       barwid = width -4;
       offscreenImage = createImage(width,height);
       offscreenGraphics = offscreenImage.getGraphics();

    }

   public void start() {
          if ( t == null ) {
             t = new Thread( this );
             t.setPriority( Thread.MIN_PRIORITY );
             threadSuspended = false;
             t.start();
          }
          else {
             if ( threadSuspended ) {
                threadSuspended = false;
                synchronized( this ) {
                   notify();
                }
             }
          }
       }

   public void stop() {
          threadSuspended = true;
       }
public void paint(Graphics g){

         int x0,y0,tick=1;

         x0=getX()+(int)(width/2.0);
         y0=getY()+(int)((height+25)/2.0);
          ranx=x0+(int) (2*rad*Math.random()-rad);
          rany=y0+ (int) (2*rad*Math.random()-rad);

 

          offscreenGraphics.setColor(Color.green);
          offscreenGraphics.drawLine(ranx, rany, ranx, rany);
          pcnt++;
          radi=Math.pow(ranx-x0, 2.0)+Math.pow(rany-y0, 2.0);

          if(radi < krad){
              offscreenGraphics.setColor(Color.yellow);
              offscreenGraphics.drawLine(ranx, rany, ranx, rany);
              pival=4.0 * (double)pin/pcnt;

 

              pin++;
          }
           diff=pival-3.14159265;

           offscreenGraphics.setColor(Color.gray);

          offscreenGraphics.fillRect(xbar, ybar, barwid, barheigt);
          offscreenGraphics.setColor(Color.white);
          offscreenGraphics.drawString(""+bartop, xbar, ybar+8);
          offscreenGraphics.drawString(""+barbot, barwid-22, ybar+8);
          offscreenGraphics.drawString(""+barmid, (barwid-22)/2, ybar+8);

          barval = ((barwid-xbar-22)/(barbot-bartop))*(pival-bartop)  ;
              line=(int) barval;
          offscreenGraphics.setColor(Color.orange);
          offscreenGraphics.drawLine(line, ybar, line,barheigt);

           g.drawImage(offscreenImage, 0, 0, this);
}

public void run() {
    try {
        while(true){
        repaint();
        t.sleep(0);

        }
    } catch (InterruptedException e) {
        // TODO: handle exception
    }

}

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

    private void pause(int pauseLength) {
        try { Thread.sleep(pauseLength); } catch (InterruptedException e) {}
    }

}

Integrating Fortran with Java in Windows

Fortran from Java

A lot has been written about mixing java with Fortran, but everything scattered and isn’t very current and most of it not for windows environment.

So here’s a little start up beginners guide to integrate java and Fortran in windows.

For Fortran compilation i am using Intel Fortran Compiler and java is java. Hope this PowerPoint will help you get started..
or watch it from here

Your smartphone matches Mars rover

Curiosity may be the most sophisticated spacecraft ever sent to another planet, but the 900-kilogram rover runs on a computer with the same power as a smartphone. The main computer that controls the rover’s post-landing software has a processing speed of just 200 megahertz, less than half the power of the latest iPhones.

Source: Iphone and Mars Rover

And how we are using the power of our smart phones!!

Industry best practice and fortran

I don’t know about you but when I know something is undone and unfinished, I don’t feel so enthusiastic in doing anything else. This is the current state of my mind. But this post isn’t about me.

Last saturday read about lisping at jpl and I felt the same about how fortran is shoved around in the name of best practice. . Here’s the relevant paragraph.

The situation is particularly ironic because the argument that has been advanced for discarding Lisp in favor of C++ (and now for Java) is that JPL should use “industry best practice.” The problem with this argument is twofold: first, we’re confusing best practice with standard practice. The two are not the same. And second, we’re assuming that best (or even standard) practice is an invariant with respect to the task, that the best way to write a word processor is also the best way to write a spacecacraft control system. It isn’t.

This is the same argument that is thrown for fortran. I agree to the second point mentioned above. There isn’t an universal language for all tasks. Specific tasks and application require specific languages. Java has it’s use and so does fortran.

What are your thoughts?

The story behind……

When I first encountered image handing while learning java, I skipped it. Thinking I will be using java for serious engineering application, I dismissed learning loading and displaying images.

Then while browsing I encountered this java EngineSim applet by NASA. Here I saw the UI utility for the image handling functionality in java.

As I went back to the original tutorial on loading image, it was Holi time and that was a perfect opportunity to put the new learning to use.

That’s the story behind Happy Holi applet.

Here’s the java applet code. Download the full Holi Hai Java Applet Code Click control S once the page loads.

On Wasting time and inefficiency

From the new rules blog

Wasting time and inefficiencies are the way to discovery. When Condé Nast’s editorial director Alexander Liberman was challenged on his inefficiencies in producing world-class magazines such The New Yorker, Vanity Fair, and Architectural Digest, he said it best: “I believe in waste. Waste is very important in creativity.”

Out of many things, inefficiencies have helped me learn java and pgplot at a faster rate.

Trying things, creating silly programs has produced so many wasteful programs, but each has sharpened the skills.

Serendipity and Springboard

If my last post attention to irrelevant gave an impression that I am struggling with java, you are wrong.

I am enjoying every bit of it. It’s not every day one can pick up a new skill. So I am enjoying the challenge and knowing the fact java is the most popular language is icing on the cake.

The concept of applet is great and I had the most fun in it. Seriously, don’t be surprised if in few weeks time, this blog is filled with applets unlike the pgplot programs.

From procedural to objects, slowly brain is beginning to think in objects.

I see learning oops essential as I know this will be useful in my fortran programs too.

Fortran 2003 and 2008 have oops features that i have tinkered with, but never did that seriously in any of my programs. This serendipity of learning java will help spur that.

After absorbing oops with java, I hope I will take up oops in fortran and will take next leap in fortran.

I remember springboards used at the karate class that I attended when I was a kid. A strong push downward, gave us enough power to jump higher. Java is becoming that same springboard for me.

Holi and How i was not able to resist myself?

I was not able to resist myself.

A week back I thought I won’t do anything like this on Holi. Decided to just wish like a normal person with a simple Internet copied picture and abstain from doing any coding.

The abstain worked only till the day of Holi. Once I began getting those static greetings, I fired eclipse and here’s the result.

A java applet to play Holi on your screen


image

Click on the picture and enjoy. Happy Holi!!

the pursuit of Java…..

I was just out of college and working at NAL. At the same time my roommate and friend was working on some java project in his office. That was the time, i began tinkering with java. Working from the usual basics, i was doing good, but then the Object oriented programming (OOP) stuff came and i got lost in the jungle of oops jargon. With VB/Fortran and my own tasks at NAL hitting in, i soon moved on, abandoning java, until now.

Pursuit of Java

This year began with the my second pursuit of java.

This time rushing through the basics, i plunged into Java with oops. This is what i think is the right approach. Transiting from procedural language to object oriented language, my mind in going everywhere. And this is where Daniel Stefankovic’s came to my help. or more precisely his course notes on Java.

Professor Daniel Stefankovic of The University of Chicago’s department of Computer Science had this java course notes.

I like them. They are short and straight. They start by exciting you and then delving into details. Just the way i like to learn something. And has lots of problems to hone the knowledge gained.

Visit the site to download the 7 PowerPoint that will help you get started in java or see then here.

Here are the 7 PowerPoint embedded for you to learn java.

Day 1

Day 2

Day 3

Day 4

Day 5

Day 6

Day 7

Nice resource if you want to pursue learning JAVA, the most popular language now.

Most Popular Programming Language of Aeronautical Graduates

Inspired by the last post Popularity of programming languages,  I wanted to do my own survey on what language aeronautical graduates of India are coding in.

Luckily i am part of a ongoing campus activity for aeronautical graduates, which gave me access to some data.

So here’s the result.

image

The sample size was 186.

image

C is the clear winner with 73% of the respondents using it, while java, which was the first  in the Popularity of programming languages survey, was used by 3% of the respondents.

Since majority of the specialization was avionics domain, so the bias towards c is understandable.

Was surprised that just 22% have used Fortran, considering 90% of Aerospace/Aeronautical/Engineering code is in that language.