Accueil > > > 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
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
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
IMAGINE CUP 2012, MAKE A SIGN EN FINALEIMAGINE CUP 2012, MAKE A SIGN EN FINALE par junarnoalg
Voilà qui est fait, la nouvelle est officielle ! L'équipe belge "Make a Sign" va au pays des kangourous défendre son projet dans la catégorie Software Design. http://www.imaginecup.com/CompetitionsContent/Competition/WorldwideFinalists.aspx V...
Cliquez pour lire la suite de l'article par junarnoalg KINECT 1.5 IS OUT !KINECT 1.5 IS OUT ! par Vko
La version 1.5 du Kinect For Microsoft vient tout juste de sortir ! Plein de nouveautés: Tracking de squelette en Near Mode Détection en position assise Détection faciale avec un SDK dédié Documentation et des guideline (enfin) Un out...
Cliquez pour lire la suite de l'article par Vko LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) par richardc
Mise à jour des Web API du 14 Mai
Réservez dès maintenant votre journée du 20 juin pour le Windows Azure Dev Camp 2012 à Paris
Mise à jour de Team Foundation Service
MechCommander 2 sur Windows 8
Entity Framework 5 Release Candidate e...
Cliquez pour lire la suite de l'article par richardc REACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITERREACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITER par Groc
Une mauvaise utilisation de rx lors de l'écriture d'une couche d'accès à des services peut conduire à des cas embarassants avec des erreurs mal gérées, des appels qui ne partent lorsqu'ils le devraient, et même des résultats incorrects . le tout nuis...
Cliquez pour lire la suite de l'article par Groc SHAREPOINT BLOG SITE, PROBLèME D'ARCHIVESSHAREPOINT BLOG SITE, PROBLèME D'ARCHIVES par junarnoalg
Dernièrement, nous avons migré le site
myTIC
vers un nouveau serveur SharePoint 2010. Dans les contenus que nous vouloins récupérer, nous avions un certain nombre de blogs.
Nous avons utilisé les commandes Power...
Cliquez pour lire la suite de l'article par junarnoalg
Forum
RE : SAC A DOS RE : SAC A DOS par hadjkaddour
Cliquez pour lire la suite par hadjkaddour
Logiciels
sDEVIS-FACTURES vlPRO (8.1.0.3)SDEVIS-FACTURES VLPRO (8.1.0.3)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO 974 Application Server (12.2.4.6)974 APPLICATION SERVER (12.2.4.6)Développez de puissantes applications dans un environnement de 'cloud computing', clusterisé, séc... Cliquez pour télécharger 974 Application Server vPicture (1.4.2.1)VPICTURE (1.4.2.1)Avec vPicture, hébergez vos images facilement et rapidement.
vPicture est un utilitaire simple, ... Cliquez pour télécharger vPicture Easy-Planning (2.2.1.6)EASY-PLANNING (2.2.1.6)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté au... Cliquez pour télécharger Easy-Planning COM-BACKUP (2.0)COM-BACKUP (2.0)
COM-BACKUP est un logiciel de sauvegarde qui permet de planifier les sauvegardes de vos dossiers ...
Cliquez pour télécharger COM-BACKUP
|