Skip to content

Instantly share code, notes, and snippets.

@leegao
Created November 14, 2010 00:42
Show Gist options
  • Save leegao/675784 to your computer and use it in GitHub Desktop.
Save leegao/675784 to your computer and use it in GitHub Desktop.
Calculates the flux over a Cat
// Calculates the flux of f(x,y) = x^2/2 + y^2/2 over a cat. CAN I HAZ SUM FLUXZ?
#include "flux.h"
double M(double x, double y){
return x; // X component of our gradient field.
}
double N(double x, double y){
return y; // Y component of our gradient field.
}
int main(){
B _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ R R _ _ _ _ _ _ _ _ _ _ P
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ R R R _ _ _ _ _ _ _ _ _ P
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ R R _ _ _ _ _ _ _ _ _ P
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ R R _ _ _ _ _ _ _ _ _ P
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ R R _ _ _ _ _ _ _ _ P
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ R R R _ _ _ _ _ _ _ P
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ R R R _ _ _ _ _ _ _ P
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ R R R _ _ _ _ _ _ _ P
_ R R _ R R _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ R R R _ _ _ _ _ _ _ P
_ R R R R R R R R R _ _ _ _ _ _ _ _ _ _ _ _ _ _ R R R _ _ _ _ _ _ _ P
_ R R R R R R R R R R R R R _ _ _ _ R R R R R R R R R R _ _ _ _ _ _ _ P
_ R R R R R R R R R R R R R R R R R R R R R R R R R R R _ _ _ _ _ _ _ _ P
R R R R R R R R R R R R R R R R R R R R R R R R R R R R _ _ _ _ _ _ _ P
R R R R R R R R R R R R R R R R R R R R R R R R R R R R R _ _ _ _ _ _ P
R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R _ _ _ _ _ _ P
R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R _ _ _ _ _ _ P
_ R R _ _ _ _ R R R R R R R R R R R R R R R R R R R R R R R R _ _ _ _ P
_ _ _ _ _ _ _ _ R R R R R R R R R R R R R R R R R R R R R R R _ _ _ _ P
_ _ _ _ _ _ _ _ R R R R R R R R R R R R R R R R R R R R R R R R R R _ _ P
_ _ _ _ _ _ R R R R R R R R R R R R R R R R R R R R R R R R R R R R R P
_ _ _ _ _ _ R R R R R R R R R R R R R R R R R R R R R R R R R R R R R _ P
_ _ _ _ _ R R R R _ R R R _ _ _ _ _ _ _ R R R R R _ R R R R R R R P
_ _ _ _ _ R R R _ _ R R R _ _ _ _ _ _ _ R R R R R R _ _ _ _ R R R P
_ _ _ _ _ R R _ _ R R R R _ _ _ _ _ _ R R R R R R _ _ _ _ _ R R R _ P
_ _ _ _ _ _ _ _ _ R R R R _ _ _ _ _ _ R R R R _ _ _ _ _ _ _ R R R R P
_ _ _ _ _ _ _ _ _ R R R _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ R R R R E
}
#ifndef FLUX_H_
#define FLUX_H_
double M(double x, double y);
double N(double x, double y);
double Flux (double x, double y, double d){return M(x+d, y) - M(x, y) + N(x, y+d) - N(x, y);}
double Circulation(double x, double y, double d){return - N(x+d, y) + N(x, y) + M(x, y+d) - M(x, y);}
int X = 0, Y = 0, len = 0, max = 0;
int* list;
#define B list?free(list):0;list = (int*)malloc(sizeof(int));_
#define _ X++;
#define R X++;list[len]=hash(X,Y);list=(int*)realloc(list, (1+ ++len) * sizeof(int));list[len]=0;
#define P max=X>max?X:max;X=0;Y++;
#define E P X=max;printFlux();
unsigned int hash(unsigned int x, unsigned int y){
return ((x << 19) | (y << 7));
}
int find(int* list, int target){
while (*list && *list != target) list++;
return *list;
}
double AreaIntegrator(int X, int Y, double d){
float integral = 0.0;
int x, y;
for (x = 0; x < X; x++){
float slice = 0.0;
for (y = 0; y < Y; y++)
if (find(list, hash(x,y)))
slice += Flux(x, y, d);
integral += slice*d;
}
return integral;
}
void printFlux(){
printf("%f",AreaIntegrator(X,Y,0.05));
}
#endif /* FLUX_H_ */
@cwgreene
Copy link

Hey you might want to change B to

#define B list ? free(list) : 0; list = (int*)malloc(sizeof(int));_

@leegao
Copy link
Author

leegao commented Nov 15, 2010

Thanks, I haven't considered that before

@cwgreene
Copy link

Wait, it looks like you're integrating over a half circle there (what remains if you remove the spaces. Is that as intended?
*Sorry, that was in reference to the previous circle glyph. Don't forget the _ between the ears.

@leegao
Copy link
Author

leegao commented Nov 15, 2010

@cwgreene: It's actually suppose to be an approximation of a circle with four extra blocks (near the equator) to make it look prettier:

            R R R R            
       R R R R R R R R R       
    R R R R R R R R R R R R    
  R R R R R R R R R R R R R R  
 R R R R R R R R R R R R R R R 
 R R R R R R R R R R R R R R R 
R R R R R R R R R R R R R R R R R
R R R R R R R R R R R R R R R R R
R R R R R R R R R R R R R R R R R
R R R R R R R R R R R R R R R R R
 R R R R R R R R R R R R R R R 
 R R R R R R R R R R R R R R R 
  R R R R R R R R R R R R R R  
    R R R R R R R R R R R R    
        R R R R R R R R        
            R R R R            

also, thanks for pointing that out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment