Shape, des dessin dans un fenêtre [code et compil avec vc++ 6.0]

Description

IL va dessiner 2 figures, un rectangle et une ellipse

Source / Exemple :


// 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

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.