begin process at 2012 05 27 19:16:21
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Graphique

 > SHAPE, DES DESSIN DANS UN FENÊTRE [CODE ET COMPIL AVEC VC++ 6.0]

SHAPE, DES DESSIN DANS UN FENÊTRE [CODE ET COMPIL AVEC VC++ 6.0]


 Information sur la source

Note :
7 / 10 - par 2 personnes
7,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Graphique Niveau :Initié Date de création :05/03/2003 Date de mise à jour :05/03/2003 00:05:58 Vu / téléchargé :5 222 / 32 657

Auteur : D1m3x

Ecrire un message privé
Commentaire sur cette source (0)
Ajouter un commentaire et/ou une note

 Description

IL va dessiner 2 figures, un rectangle et une ellipse  

Source

  • // Vous allez devoir créer plusierus document, mais je vous les mets dans le
  • // zip
  • /* TOUT D'ABORD LE FICHIER D'EN-TETE: CPoint.h */
  • struct Point
  • {
  • // Constructeurs:
  • Point() { x = 0; y = 0; };
  • Point(int ix, int iy) { x =ix; y = iy; };
  • // Attributs:
  • int x;
  • int y;
  • };
  • /* MAINTENANT LE FICHIER D'EN-TETE: CRect.h */
  • #include "CPoint.h"
  • struct Rect
  • {
  • //Constructeurs:
  • Rect() { m_ptTopLeft = Point(); m_ptBotRight = Point(10,10); }
  • Rect(Point tl, Point br) { m_ptTopLeft = tl; m_ptBotRight = br; };
  • // Attributs:
  • Point m_ptTopLeft;
  • Point m_ptBotRight;
  • //Fonctions membres:
  • void SetRect(Point tl, Point br)
  • {m_ptTopLeft = tl; m_ptBotRight = br; };
  • void SetRect(int x1, int y1, int x2, int y2) {
  • m_ptTopLeft.x = x1; m_ptTopLeft.y = y1;
  • m_ptBotRight.x = x2; m_ptBotRight.y = y2; };
  • };
  • /* MAINTENANT LE FICHIER D'EN-TETE: Shape.h
  • #include "CRect.h"
  • // Constantes pour génération aléatoire de types de figure
  • const int NUM_TYPES = 2;
  • // Types de figure possible
  • enum ShpType
  • {
  • shpRectangle,
  • shpEllipse
  • // On peut en ajouter d'autres
  • };
  • // Classe Shape
  • struct Shape
  • {
  • Rect m_rectShape; // rectangle circonscrit
  • ShpType m_typeShape; // rectangle, ellipse, etc.
  • };
  • /* MAINTENANT LE FICHIER SOURCE: Shape.cpp */
  • #include "Shape.h"
  • #include <stdio.h> // Ppour printf, sprintf
  • #include <assert.h> // Pour assert
  • #include <stdlib.h> // Pour rand, srand, abs
  • #include <time.h> // Pour time
  • #include <string.h> // Pour strcpy
  • // Prototype de fonctiion globale
  • void DrawShape(Shape* pS);
  • void MoveShape(Shape* pS, Point p);
  • int RandomCoord();
  • ShpType RandomType();
  • const int COORD_MAX = 1000;
  • // main
  • int main(int argc, char* argv[])
  • {
  • // Crée une figure sur la pile
  • Shape shp1;
  • shp1.m_rectShape = Rect(Point(20,20), Point(50,50));
  • shp1.m_typeShape = shpRectangle;
  • DrawShape(&shp1);
  • // Déplace une figure
  • MoveShape(&shp1, Point(25,25));
  • DrawShape(&shp1);
  • // Crée une figure sur le tas
  • Shape* pShp2 = new Shape;
  • assert(pShp2 != NULL);
  • pShp2 ->m_rectShape = Rect(Point(100,100), Point(150,150));
  • pShp2 ->m_typeShape = shpEllipse;
  • DrawShape(pShp2);
  • delete pShp2;
  • // Crée 20 figures aléatoires dans le tableau
  • Point pt1, pt2;
  • Shape* arShps[20];
  • srand((unsigned)time(NULL)); // Nombre aléatoire
  • for(int i = 0; i < 20; i++)
  • {
  • // Crée une nouvelle figure
  • Shape* pShp = new Shape;
  • assert(pShp != NULL);
  • // Propose des coordonnée
  • pt1 = Point(RandomCoord(), RandomCoord());
  • pt2 = Point(RandomCoord(), RandomCoord());
  • pShp ->m_rectShape = Rect(pt1, pt2);
  • // Précise si rectangle ou ellipse
  • pShp ->m_typeShape = RandomType();
  • // Ajoute un tableau
  • arShps[i] = pShp;
  • // "Dessine" la figure sous forme de chaîne
  • DrawShape(arShps[i]);
  • }
  • // Déplace une figure
  • MoveShape(arShps[0], Point(20,20));
  • DrawShape(arShps[0]);
  • // Efface toutes les figures
  • for(int j = 0; j < 20; j++) delete arShps[j];
  • return 0;
  • } // Fin du main
  • // Définitions des fonctions globales.
  • // "Dessine" la figure
  • void DrawShape(Shape* pS)
  • {
  • Rect rect = pS ->m_rectShape;
  • int x1 = rect.m_ptTopLeft.x;
  • int y1 = rect.m_ptTopLeft.y;
  • int x2 = rect.m_ptBotRight.x;
  • int y2 = rect.m_ptBotRight.y;
  • // Détermine le type de figure sous forme de chaîne
  • char szType[20];
  • switch(pS ->m_typeShape)
  • {
  • case shpRectangle: strcpy(szType, "retangle");
  • break;
  • case shpEllipse: strcpy(szType, "ellipse");
  • break;
  • default: strcpy(szType, "erreur type de figure");
  • };
  • // "Dessine" la figure sous forme de chaîne
  • // Exemple: "rectangle à (34, 76, 987, 800) "
  • printf("%s à (%d, %d, %d, %d)\n", szType, x1, y1, x2, y2);
  • } // Fin de DrawShape
  • // Déplace la ifgure à la nouvelle position p
  • void MoveShape(Shape *pS, Point p /* New m_TopLeft*/)
  • {
  • Rect rect = pS ->m_rectShape;
  • int width = abs(rect.m_ptBotRight.x - rect.m_ptTopLeft.x);
  • int height = abs(rect.m_ptBotRight.y - rect.m_ptTopLeft.y);
  • // Nouveau CSG m_TopLeft = p.
  • pS ->m_rectShape.m_ptTopLeft = p;
  • // Nouveau CID m_ptBotRight calculé à partir de p
  • // en utilisant la taille de la figure
  • pS ->m_rectShape.m_ptBotRight = Point(p.x + width, p.y + height);
  • } // Fin de MoveShape
  • // Fonctions auxiliaires globale
  • // Génère une coordonée poitive aléatoire
  • // dans une zone de dessin COORD_MAX x COORD_MAN
  • int RandomCoord()
  • {
  • // Fonde les nouvelles coordonées sur les anciennes
  • static int nLastCoord; // Initialisée
  • // automatiquement à 0
  • // puis modifié lors de
  • // chaque appel
  • // Obtient un nombre aléatoire compris entre 0 et RAND_MAX (32767)
  • int nNextCoord = rand();
  • int nFudge = rand() % 100; // Génère facteur entre
  • // 0 et 99
  • nLastCoord = (nNextCoord > nLastCoord ? nNextCoord : nLastCoord);
  • // Limite le nombre à une valeur entre 0 et COORD_MAX - 1(inclus)
  • nLastCoord = (nLastCoord + nFudge) % COORD_MAX;
  • return nLastCoord;
  • } // Fin de RandomCoord
  • // Le nombre aléatoire génère un type de figure
  • ShpType RandomTypz()
  • {
  • // 0 à 1 (= shpRetangle à shpEllipse)
  • return (ShpType)(rand() % NUM_TYPES);
  • }
// Vous allez devoir créer plusierus document, mais je vous les mets dans le
// zip

/* TOUT D'ABORD LE FICHIER D'EN-TETE: CPoint.h */

struct Point
{
	// Constructeurs:
	Point() { x = 0; y = 0; };
	Point(int ix, int iy) {	x =ix; y = iy; };

	// Attributs:
	int x;
	int y;
};

/* MAINTENANT LE FICHIER D'EN-TETE: CRect.h */

#include "CPoint.h"

struct Rect
{
	//Constructeurs:
	Rect() { m_ptTopLeft = Point(); m_ptBotRight = Point(10,10); }
	Rect(Point tl, Point br) { m_ptTopLeft = tl; m_ptBotRight = br; };

	// Attributs:
	Point m_ptTopLeft;
	Point m_ptBotRight;

	//Fonctions membres:
	void SetRect(Point tl, Point br)
	{m_ptTopLeft = tl; m_ptBotRight = br; };
	void SetRect(int x1, int y1, int x2, int y2) {
		m_ptTopLeft.x = x1; m_ptTopLeft.y = y1;
		m_ptBotRight.x = x2; m_ptBotRight.y = y2; };
};

/* MAINTENANT LE FICHIER D'EN-TETE: Shape.h

#include "CRect.h"

// Constantes pour génération aléatoire de types de figure
const int NUM_TYPES = 2;

// Types de figure possible
enum ShpType
{
	shpRectangle,
	shpEllipse
	// On peut en ajouter d'autres
};

// Classe Shape
struct Shape
{
	Rect m_rectShape; // rectangle circonscrit
	ShpType m_typeShape; // rectangle, ellipse, etc.
};

/* MAINTENANT LE FICHIER SOURCE: Shape.cpp */

#include "Shape.h"
#include <stdio.h>   // Ppour printf, sprintf
#include <assert.h>  // Pour assert
#include <stdlib.h>  // Pour rand, srand, abs
#include <time.h>    // Pour time
#include <string.h>  // Pour strcpy

// Prototype de fonctiion globale
void DrawShape(Shape* pS);
void MoveShape(Shape* pS, Point p);
int RandomCoord();
ShpType RandomType();

const int COORD_MAX = 1000;

// main
int main(int argc, char* argv[])
{
	// Crée une figure sur la pile
	Shape shp1;
	shp1.m_rectShape = Rect(Point(20,20), Point(50,50));
	shp1.m_typeShape = shpRectangle;
	DrawShape(&shp1);

	// Déplace une figure
	MoveShape(&shp1, Point(25,25));
	DrawShape(&shp1);

	// Crée une figure sur le tas
	Shape* pShp2 = new Shape;
	assert(pShp2 != NULL);
	pShp2 ->m_rectShape = Rect(Point(100,100), Point(150,150));
	pShp2 ->m_typeShape = shpEllipse;
	DrawShape(pShp2);

	delete pShp2;

	// Crée 20 figures aléatoires dans le tableau
	Point pt1, pt2;
	Shape* arShps[20];
	srand((unsigned)time(NULL)); // Nombre aléatoire
	for(int i = 0; i < 20; i++)
	{
		// Crée une nouvelle figure
		Shape* pShp = new Shape;
		assert(pShp != NULL);
		// Propose des coordonnée
		pt1 = Point(RandomCoord(), RandomCoord());
		pt2 = Point(RandomCoord(), RandomCoord());
		pShp ->m_rectShape = Rect(pt1, pt2);
		// Précise si rectangle ou ellipse
		pShp ->m_typeShape = RandomType();
		// Ajoute un tableau
		arShps[i] = pShp;
		// "Dessine" la figure sous forme de chaîne
		DrawShape(arShps[i]);
	}
	// Déplace une figure
	MoveShape(arShps[0], Point(20,20));
	DrawShape(arShps[0]);

	// Efface toutes les figures
	for(int j = 0; j < 20; j++) delete arShps[j];
	 
	return 0;
} // Fin du main

// Définitions des fonctions globales.
// "Dessine" la figure 
void DrawShape(Shape* pS)
{
	Rect rect = pS ->m_rectShape;
	int x1 = rect.m_ptTopLeft.x;
	int y1 = rect.m_ptTopLeft.y;
	int x2 = rect.m_ptBotRight.x;
	int y2 = rect.m_ptBotRight.y;
	// Détermine le type de figure sous forme de chaîne
	char szType[20];
	switch(pS ->m_typeShape)
	{
		case shpRectangle: strcpy(szType, "retangle");
			break;
		case shpEllipse: strcpy(szType, "ellipse");
			break;
		default: strcpy(szType, "erreur type de figure");
	};

	// "Dessine" la figure sous forme de chaîne
	// Exemple: "rectangle à (34, 76, 987, 800) "
	printf("%s à (%d, %d, %d, %d)\n", szType, x1, y1, x2, y2);
} // Fin de DrawShape

// Déplace la ifgure à la nouvelle position p
void MoveShape(Shape *pS, Point p /* New m_TopLeft*/)
{
	Rect rect = pS ->m_rectShape;
	int width = abs(rect.m_ptBotRight.x - rect.m_ptTopLeft.x);
	int height = abs(rect.m_ptBotRight.y - rect.m_ptTopLeft.y);

	// Nouveau CSG m_TopLeft = p.
	pS ->m_rectShape.m_ptTopLeft = p;
	// Nouveau CID m_ptBotRight calculé à partir de p
	// en utilisant la taille de la figure
	pS ->m_rectShape.m_ptBotRight = Point(p.x + width, p.y + height);
} // Fin de MoveShape

// Fonctions auxiliaires globale

// Génère une coordonée poitive aléatoire
// dans une zone de dessin COORD_MAX x COORD_MAN 
int RandomCoord()
{
	// Fonde les nouvelles coordonées sur les anciennes
	static int nLastCoord; // Initialisée
	                       // automatiquement à 0
	                       // puis modifié lors de 
	                       // chaque appel

	// Obtient un nombre aléatoire compris entre 0 et RAND_MAX (32767)
	int nNextCoord = rand();
	int nFudge = rand() % 100; // Génère facteur entre
	                           // 0 et 99
	nLastCoord = (nNextCoord > nLastCoord ? nNextCoord : nLastCoord);

	// Limite le nombre à une valeur entre 0 et COORD_MAX - 1(inclus)
	nLastCoord = (nLastCoord + nFudge) % COORD_MAX;
	return nLastCoord;
} // Fin de RandomCoord

// Le nombre aléatoire génère un type de figure
ShpType RandomTypz()
{
	// 0 à 1 (= shpRetangle à shpEllipse)
	return (ShpType)(rand() % NUM_TYPES);
}  

 Conclusion

PAs de bug vec VC++ 6.0  

 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Sources du même auteur

Source avec Zip Source avec une capture HACHAGE MD5 AVEC INTERFACE QT [VC++ 6.0]
Source avec Zip TUTORIAL SUR LA SDL, EN 4 PARTIE, 4 DOCUMENT WORD (.DOC)
Source avec Zip CRYPTAGE AVEC MD5!!! [VC++ 6.0]
Source avec Zip COMMENT FAIRE UNE DLL UTILISABLE DANS VOS PROJET C\C++ -> RE...
Source avec Zip CCONSOLEMENU -> CLASSE POUR LA CONSTRUCTION D'UN MENU DANS U...

 Sources de la même categorie

Source avec Zip Source avec une capture PLANNING D'EQUIPE par grephit
Source avec Zip APPLICATION DE DESSIN DE QUELQUES FIGURES par laguchori
Source avec Zip Source avec une capture HDR EXPOSURE FUSION par mecrosoft
Source avec Zip Source avec une capture IRC CLIENT MULTISERVEUR EN MFC (TXIRC) par TeniX
Source avec Zip ENTETE DU FICHIER BMP (BIPMAP) par k.Lutchi

Commentaires et avis

Aucun commentaire pour le moment.

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

Photothèque

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,374 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales