IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++ Discussion :

Transformation du code java en c++


Sujet :

C++

Vue hybride

king_prog Transformation du code java... 17/05/2009, 15h15
Lucien63 avec java2cpp.exe 17/05/2009, 15h44
king_prog ou on peut trouver... 17/05/2009, 17h54
Alp N'as-tu pas lu ce que Loïc et... 17/05/2009, 18h01
JolyLoic Sur quels points bloques-tu ?... 17/05/2009, 16h38
Alp Et trop de classes partout... 17/05/2009, 17h07
Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Candidat au Club
    Inscrit en
    Mai 2009
    Messages
    2
    Détails du profil
    Informations forums :
    Inscription : Mai 2009
    Messages : 2
    Par défaut Transformation du code java en c++
    salut,

    je suis un débutant en C++ et je ne peux pas comment transformez du code java en c++ et voila le code java à transformez:
    La classe Snake (attributs+constructeur)
    Code java :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    public class Snake {
     
    	// Points of the snake
    	private List<Point> snake;
     
    	// Length of the snake (euclidean distance)
    	private double snakelength=0;
     
    	// size of the image (and of the 2 arrays below)
    	private int width=0,height=0;
     
    	// gradient value (modulus)
    	private int[][] gradient;
     
    	// gradient flow (modulus)
    	private int[][] flow;
     
    	// 3x3 neighborhood used to compute energies
    	private double[][] e_uniformity = new double[3][3];
    	private double[][] e_curvature  = new double[3][3];
    	private double[][] e_flow       = new double[3][3];
    	private double[][] e_inertia    = new double[3][3];
     
    	// auto add/remove points to the snake
    	// according to distance between points
    	private boolean AUTOADAPT=true;
    	private static int AUTOADAPT_LOOP=10;
    	private static int AUTOADAPT_MINLEN=8;
    	private static int AUTOADAPT_MAXLEN=16;
     
    	// maximum number of iterations (if no convergence)
    	private static int MAXITERATION = 1000;
     
    	// coefficients for the 4 energy functions
    	public double alpha=1.1, beta=1.2, gamma=1.5, delta=3.0;
     
    	// alpha = coefficient for uniformity (high => force equals distance between points)
    	// beta  = coefficient for curvature  (high => force smooth curvature)
    	// gamma  = coefficient for flow      (high => force gradient attraction)
    	// delta  = coefficient for intertia  (high => get stuck to gradient)
     
    	/**
             * Constructor
             *
             * @param width,height size of the image and of the 2 following arrays
             * @param gradient gradient (modulus)
             * @param flow gradient flow (modulus)
             * @param points inital points of the snake
             */
    	public Snake(int width, int height, int[][] gradient, int[][] flow, Point... points) {
    		this.snake = new ArrayList<Point>(Arrays.asList(points));
    		this.gradient = gradient;
    		this.flow = flow;
    		this.width = width;
    		this.height = height;
    	}
     
     
    	// add here the other methods.
     
    }
     
     
    Les méthodes de l'algorithme "snake"
     
    Code java :
     
    /**
     * main loop
     * 
     * @return the final snake
     */
    public List<Point> loop() {
    	int loop=0;
     
    	while(step() && loop<MAXITERATION) {
    		// auto adapt the number of points in the snake
    		if (AUTOADAPT && (loop%AUTOADAPT_LOOP)==0) {
    			removeOverlappingPoints(AUTOADAPT_MINLEN);
    			addMissingPoints(AUTOADAPT_MAXLEN);
    		}
    		loop++;
    	}
     
    	// rebuild using spline interpolation
    	if (AUTOADAPT) rebuild(AUTOADAPT_MAXLEN);
     
    	return this.snake;
    }
     
    /**
     * update the position of each point of the snake
     *
     * @return true if the snake has changed, otherwise false.
     */
    private boolean step() {
    	boolean changed=false;
    	Point p = new Point(0,0);
     
    	// compute length of original snake (used by method: f_uniformity)
    	this.snakelength = getsnakelength();
     
    	// compute the new snake
    	List<Point> newsnake = new ArrayList<Point>(snake.size());
     
    	// for each point of the previous snake
    	for(int i=0;i<snake.size();i++) {
    		Point prev = snake.get((i+snake.size()-1)%snake.size());
    		Point cur  = snake.get(i);
    		Point next = snake.get((i+1)%snake.size());
     
    		// compute all energies
    		for(int dy=-1;dy<=1;dy++) {
    			for(int dx=-1;dx<=1;dx++) {
    				p.setLocation(cur.x+dx, cur.y+dy);
    				e_uniformity[1+dx][1+dy] = f_uniformity(prev,next,p);
    				e_curvature[1+dx][1+dy]  = f_curvature(prev,p,next);
    				e_flow[1+dx][1+dy]       = f_gflow(cur,p);
    				e_inertia[1+dx][1+dy]    = f_inertia(cur,p);
    			}
    		}
     
    		// normalize energies
    		normalize(e_uniformity);
    		normalize(e_curvature);
    		normalize(e_flow);
    		normalize(e_inertia);
     
    		// find the point with the minimum sum of energies
    		double emin = Double.MAX_VALUE, e=0;
    		int x=0,y=0;
    		for(int dy=-1;dy<=1;dy++) {
    			for(int dx=-1;dx<=1;dx++) {
    				e = 0;
    				e+= alpha * e_uniformity[1+dx][1+dy]; // internal energy
    				e+= beta  * e_curvature[1+dx][1+dy];  // internal energy
    				e+= gamma * e_flow[1+dx][1+dy];       // external energy
    				e+= delta * e_inertia[1+dx][1+dy];    // external energy
     
    				if (e<emin) { emin=e; x=cur.x+dx; y=cur.y+dy; }
    			}
    		}
     
    		// boundary check
    		if (x<1) x=1;
    		if (x>=(this.width-1)) x=this.width-2;
    		if (y<1) y=1;
    		if (y>=(this.height-1)) y=this.height-2;
     
    		// compute the returned value
    		if (x!=cur.x || y!=cur.y) changed=true;
     
    		// create the point in the new snake
    		newsnake.add(new Point(x,y));
    	}
     
    	// new snake becomes current
    	this.snake=newsnake;
     
    	return changed;
    }
     
    // normalize energy matrix
    private void normalize(double[][] array3x3) {
    	double sum=0;
    	for(int i=0;i<3;i++)
    		for(int j=0;j<3;j++)
    			sum+=Math.abs(array3x3[i][j]);
     
    	if (sum==0) return;
     
    	for(int i=0;i<3;i++)
    		for(int j=0;j<3;j++)
    			array3x3[i][j]/=sum;
    }
     
    private double getsnakelength() {
    	// total length of snake
    	double length=0;
    	for(int i=0;i<snake.size();i++) {
    		Point cur   = snake.get(i);
    		Point next  = snake.get((i+1)%snake.size());
    		length+=distance2D(cur, next);
    	}
    	return length;
    }
     
    private double distance2D(Point A, Point B) {
    	int ux = A.x-B.x;
    	int uy = A.y-B.y;
    	double un = ux*ux+uy*uy;
    	return Math.sqrt(un);
    }
     
     
    Les méthodes des fonctions d'energie:
     
    Code java :
     
    private double f_uniformity(Point prev, Point next, Point p) {
     
    	// length of previous segment
    	double un = distance2D(prev, p);
     
    	// mesure of uniformity
    	double avg = snakelength/snake.size();
    	double dun = Math.abs(un-avg);
     
    	// elasticity energy
    	return dun*dun;
    }
     
    private double f_curvature(Point prev, Point p, Point next) {
    	int ux = p.x-prev.x;
    	int uy = p.y-prev.y;
    	double un = Math.sqrt(ux*ux+uy*uy);
     
    	int vx = p.x-next.x;
    	int vy = p.y-next.y;
    	double vn = Math.sqrt(vx*vx+vy*vy);
     
    	if (un==0 || vn==0) return 0;
     
    	double cx = (vx+ux)/(un*vn);
    	double cy = (vy+uy)/(un*vn);
     
    	// curvature energy
    	double cn = cx*cx+cy*cy;
    	return cn;
    }
     
    private double f_gflow(Point cur, Point p) {
    	// gradient flow
    	int dcur = this.flow[cur.x][cur.y];
    	int dp   = this.flow[p.x][p.y];
    	double d = dp-dcur;
    	return d;
    }
     
    private double f_inertia(Point cur, Point p) {
    	double d = distance2D(cur, p);
    	double g = this.gradient[cur.x][cur.y];
    	double e = g*d;
    	return e;
    }
     
    Les méthodes du mécanisme d'auto-adaptation:
     
    Code java :
    // rebuild the snake using cubic spline interpolation
    private void rebuild(int space) {
     
    	// precompute length(i) = length of the snake from start to point #i
    	double[] clength = new double[snake.size()+1];
    	clength[0]=0;
    	for(int i=0;i<snake.size();i++) {
    		Point cur   = snake.get(i);
    		Point next  = snake.get((i+1)%snake.size());
    		clength[i+1]=clength[i]+distance2D(cur, next);
    	}
     
    	// compute number of points in the new snake
    	double total = clength[snake.size()];
    	int nmb = (int)(0.5+total/space);
     
    	// build a new snake
    	List<Point> newsnake = new ArrayList<Point>(snake.size());
    	for(int i=0,j=0;j<nmb;j++) {
    		// current length in the new snake
    		double dist = (j*total)/nmb;
     
    		// find corresponding interval of points in the original snake
    		while(! (clength[i]<=dist && dist<clength[i+1])) i++;
     
    		// get points (P-1,P,P+1,P+2) in the original snake
    		Point prev  = snake.get((i+snake.size()-1)%snake.size());
    		Point cur   = snake.get(i);
    		Point next  = snake.get((i+1)%snake.size());
    		Point next2  = snake.get((i+2)%snake.size());
     
    		// do cubic spline interpolation
    		double t =  (dist-clength[i])/(clength[i+1]-clength[i]);
    		double t2 = t*t, t3=t2*t;
    		double c0 =  1*t3;
    		double c1 = -3*t3 +3*t2 +3*t + 1;
    		double c2 =  3*t3 -6*t2 + 4;
    		double c3 = -1*t3 +3*t2 -3*t + 1;
    		double x = prev.x*c3 + cur.x*c2 + next.x* c1 + next2.x*c0;
    		double y = prev.y*c3 + cur.y*c2 + next.y* c1 + next2.y*c0;
    		Point newpoint = new Point( (int)(0.5+x/6), (int)(0.5+y/6) );
     
    		// add computed point to the new snake
    		newsnake.add(newpoint);
    	}
    	this.snake = newsnake;
    }
     
     
    private void removeOverlappingPoints(int minlen) {
    	// for each point of the snake
    	for(int i=0;i<snake.size();i++) {
    		Point cur = snake.get(i);
     
    		// check the other points (right half)
    		for(int di=1+snake.size()/2;di>0;di--) {
    			Point end  = snake.get((i+di)%snake.size());
    			double dist = distance2D(cur,end);
     
    			// if the two points are to close...
    			if ( dist>minlen ) continue;
     
    			// ... cut the "loop" part og the snake
    			for(int k=0;k<di;k++) snake.remove( (i+1) %snake.size() );
    			break;
    		}
    	}
    }
     
    private void addMissingPoints(int maxlen) {
    	// for each point of the snake
    	for(int i=0;i<snake.size();i++) {
    		Point prev  = snake.get((i+snake.size()-1)%snake.size());
    		Point cur   = snake.get(i);
    		Point next  = snake.get((i+1)%snake.size());
    		Point next2  = snake.get((i+2)%snake.size());
     
    		// if the next point is to far then add a new point
    		if ( distance2D(cur,next)>maxlen ) {
     
    			// precomputed Uniform cubic B-spline for t=0.5
    			double c0=0.125/6.0, c1=2.875/6.0, c2=2.875/6.0, c3=0.125/6.0;
    			double x = prev.x*c3 + cur.x*c2 + next.x* c1 + next2.x*c0;
    			double y = prev.y*c3 + cur.y*c2 + next.y* c1 + next2.y*c0;
    			Point newpoint = new Point( (int)(0.5+x), (int)(0.5+y) );
     
    			snake.add( i+1 , newpoint ); i--;
    		}
    	}
    }
    et merci pour vos réponses

  2. #2
    Membre émérite
    Inscrit en
    Juillet 2005
    Messages
    512
    Détails du profil
    Informations forums :
    Inscription : Juillet 2005
    Messages : 512
    Par défaut
    avec java2cpp.exe

  3. #3
    Candidat au Club
    Inscrit en
    Mai 2009
    Messages
    2
    Détails du profil
    Informations forums :
    Inscription : Mai 2009
    Messages : 2
    Par défaut
    Citation Envoyé par Lucien63 Voir le message
    avec java2cpp.exe
    ou on peut trouver java2cpp.exe?

  4. #4
    Alp
    Alp est déconnecté
    Expert confirmé

    Avatar de Alp
    Homme Profil pro
    Inscrit en
    Juin 2005
    Messages
    8 575
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Juin 2005
    Messages : 8 575
    Par défaut
    Citation Envoyé par king_prog Voir le message
    ou on peut trouver java2cpp.exe?
    N'as-tu pas lu ce que Loïc et moi disons plus haut ?

    Sinon, pour Java2CC++ : http://www.cse.buffalo.edu/~regan/cse250/Java2C++.html on dirait...

  5. #5
    Rédacteur/Modérateur
    Avatar de JolyLoic
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2004
    Messages
    5 463
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2004
    Messages : 5 463
    Par défaut
    Sur quels points bloques-tu ?
    Par ailleurs, transformer du code n'est pas forcément une bonne idée :
    - Tu risques de tomber sur des aspects de C++ que tu ne connais pas encore
    - Tu risques de prendre de mauvaises habitude, chaque langage ayant ses propres spécificités par rapport au style de codage. Par exemple, on reconnais en C++ quelqu'un qui n'a fait que du Java avant car il utilise beaucoup trop de pointeurs.
    Ma session aux Microsoft TechDays 2013 : Développer en natif avec C++11.
    Celle des Microsoft TechDays 2014 : Bonnes pratiques pour apprivoiser le C++11 avec Visual C++
    Et celle des Microsoft TechDays 2015 : Visual C++ 2015 : voyage à la découverte d'un nouveau monde
    Je donne des formations au C++ en entreprise, n'hésitez pas à me contacter.

  6. #6
    Alp
    Alp est déconnecté
    Expert confirmé

    Avatar de Alp
    Homme Profil pro
    Inscrit en
    Juin 2005
    Messages
    8 575
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Juin 2005
    Messages : 8 575
    Par défaut
    Citation Envoyé par JolyLoic Voir le message
    Par exemple, on reconnais en C++ quelqu'un qui n'a fait que du Java avant car il utilise beaucoup trop de pointeurs.
    Et trop de classes partout pour tout faire sans réel apport

Discussions similaires

  1. Outil transformant un diagramme de classe en code Java
    Par mme_chelaou dans le forum EDI et Outils pour Java
    Réponses: 2
    Dernier message: 11/03/2014, 17h38
  2. Transformer du code Delphi en Java
    Par rosana23 dans le forum Général Java
    Réponses: 1
    Dernier message: 18/07/2010, 10h40
  3. Cherche QQun aide Transformation CODE JAVA -> C#
    Par alex_nicolas dans le forum C#
    Réponses: 0
    Dernier message: 04/02/2009, 10h23
  4. transformer mon code c en java
    Par lenul79 dans le forum Débuter avec Java
    Réponses: 12
    Dernier message: 23/04/2008, 11h24
  5. transformer un code java en c++
    Par boinamib dans le forum C++
    Réponses: 2
    Dernier message: 03/07/2007, 01h41

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo