Closed Thread
Results 1 to 2 of 2

Thread: From C to Pascal

  1. #1
    Magmas is offline Newbie
    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    From C to Pascal

    Hello,
    could someone convert this to Pascal (if possible Delphi)?
    Thanks in advance


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define DIM 4	  // number of state quantities
    #define DT  1e-4   // time interval of computation
    #define G   9.81 // gravitational acceleration
    
    #define C  .5	// constant from Newtons formula for drag 
    #define R  20e-3 // radius of ball
    #define RHO 1.2 // density of air
    #define M   2.7e-3 // mass of ball
    
    #define DRAG (C*M_PI*R*R*RHO/M/2)
    
    typedef double tVector[DIM];
    typedef void(&tFCEDER)(tVector& out, tVector in);
    
    int main(int argc, char** argv);
    void eulerStep( void(&rovnice)(tVector& out, tVector in), tVector &U, double &t, double dt );
    void midPoint( void(&rovnice)(tVector& out, tVector in), tVector &U, double &t, double dt );
    void newtowDrag( tVector& dVdt, tVector V);
    void nodrag( tVector& dVdt, tVector V);
    
    double throw(tFCEDER equation, double v, double angle, double dt, bool print);
    double optimalAngle( tFCEDER drag, double v, double dt);
    
    
    int main(int argc, char** argv)
    {
    // part A
    	double v = 10.;
    	for(int i = 2; i < 18; i++){
    		double u = (i/2)/18.*M_PI;
    		throw( (i%2?nodrag:newtowDrag), v, u, DT, true);
    		printf("\n\n\n");
    	}
    // Part B
    	for(double v = 10; v <= 130; v+=20){
    		double phi_max = optimalAngle(newtowDrag, v, DT);
    		throw(newtowDrag, v, phi_max, DT, true);
    		printf("\n\n\n");
    	}
    // Part C
    	for(double v = 1; v <= 200; v++){
    		double phi_max = optimalAngle(newtowDrag, v, DT);
    		double x_max = throw(newtowDrag, v, phi_max, DT, false);
    		printf("%g %g %g\n", x_max, v, phi_max/M_PI*180.);
    	}
    	return 0;
    }
    
    double optimalAngle( tFCEDER drag, double v, double dt)
    {
    	double a = 0;
    	double b = M_PI/2;
    	double xA = 0, xB = 0, yA = 0, yB = 0;
    	while(b-a > M_PI/180./60.){
    		xA = a +   (b-a)/3;
    		xB = a + 2*(b-a)/3;
    		yA = throw(drag, v, xA, dt, false);
    		yB = throw(drag, v, xB, dt, false);
    
    		if(yA > yB)  b = xB;
    		else         a = xA;
    	}
    	return (a+b)/2;
    }
    
    double throw( tFCEDER equation, double v, double angle, double dt, bool print)
    {
    	double t = 0;
    	tVector U = { 0.,0.,v*cos(angle), v*sin(angle) };
    	while(U[1] >= 0){
    		midPoint(equation, U, t, dt);
    		if(print) printf("%g %g\n", U[0], U[1]);
    	}
    	return U[0];
    }
    
    void newtowDrag( tVector& dVdt, tVector V)
    {
    	dVdt[0] = V[2];
    	dVdt[1] = V[3];
    	dVdt[2] = -DRAG*V[2]*hypot(V[2],V[3]);
    	dVdt[3] = -DRAG*V[3]*hypot(V[2],V[3])-G;
    }
    
    void nodrag( tVector& dVdt, tVector V)
    {
    	dVdt[0] = V[2];
    	dVdt[1] = V[3];
    	dVdt[2] = 0;
    	dVdt[3] = -G;
    }
    
    
    void midPoint( tFCEDER equation, tVector &U, double &t, double dt )
    {
    	tVector df;
    	equation(df, U);
    
    	tVector midP;
    	for(int i = 0; i < DIM; i++)
    		midP[i] = U[i] + df[i]*dt/2.;
    
    	equation(df, midP);
    	for(int i = 0; i < DIM; i++)
    		U[i] += df[i]*dt;
    
    }
    void eulerStep( tFCEDER equation, tVector &U, double &t, double dt )
    {
    	tVector df;
    	equation(df, U);
    	for(int i = 0; i < DIM; i++)
    		U[i] += df[i]*dt;
    	t += dt;
    }
    Last edited by WingedPanther; 08-25-2009 at 01:31 PM. Reason: add code tags (the # button)

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: From C to Pascal

    First observation: that isn't standard C code.
    Second observation: it shouldn't be that hard to convert to Pascal/Delphi/Lazarus.

    What do you have so far?

    Free hint: #defines will turn into a const block.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Pascal
    By Hunter100 in forum Pascal and Delphi
    Replies: 13
    Last Post: 02-16-2010, 01:21 PM
  2. GUI in Pascal
    By Davide in forum Pascal and Delphi
    Replies: 7
    Last Post: 01-25-2010, 04:35 AM
  3. Help! Pascal
    By jen91 in forum Pascal and Delphi
    Replies: 1
    Last Post: 02-05-2009, 04:56 PM
  4. Dev Pascal Help
    By Pyreforge in forum Pascal and Delphi
    Replies: 3
    Last Post: 11-16-2008, 07:27 AM
  5. Dev pascal help
    By ragingfear in forum General Programming
    Replies: 3
    Last Post: 11-21-2007, 01:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts