• Aucun résultat trouvé

Introduction to Programming

N/A
N/A
Protected

Academic year: 2022

Partager "Introduction to Programming"

Copied!
2
0
0

Texte intégral

(1)

Introduction to Programming

Practical #7

1 First objects and fractals

Figure1 – Fractals. . .

In this practical, we will try some modest object oriented programming. We will transform a structure for a vector in a class and use it to draw fractals (figure 1).

1.1 Sierpinski triangle

1. Get the project :

Download the file Tp7_Initial.zip, decompress it and launch your IDE. Study the structure Vector defined in filesVector.cppandVector.h.

2. Interface with Imagine++ :

The structure Vector does not provide any graphic display. Add inmain.cpp functions drawLineand drawTriangletaking someVectoras arguments (here, a vector is used to note a plane point). Just call the regular function

void drawLine (i n t x1 , i n t y1 , i n t x2 , i n t y2 , const Color& c , i n t pen_w)

from Imagine++. The last argument controls the thickness of the pencil.

3. Sierpinski Triangle :

This is the figure chosen by ENPC for its logo. Figure 2 illustrates its construction.

Write a recursive function to draw the Sierpinski triangle. This functions takes as arguments the three

Figure 2 – Construction of Sierpinski triangle.

points of the current triangle and the thickness of the pencil. The three subtriangles are drawn with a pencil less thick.Do not forget the break condition for the recursion !

Use this function in main by giving it an initial equilateral triangle of thickness 6 pixels.

1

(2)

1.2 A class rather than a structure

4. Class vector :

Transform the structureVectorinto a class. Incorporate all functions and operators. Put in public only what is necessary, do the required modifications inmain.cpp.

5. Accessors for members :

Add accessors for reading and writing members, and use them systematically in the main program. The idea is to hide from the user of the classVectorthe details of its implementation.

6. Recursive drawing of a tree :

We are now going to draw a tree. For that, we start from a trunk and replace the second half of each branch by two branches of same length with an angle of 20 degrees with the originating branch. Figure 3 illustrates the result for different depths of recursion.

Write a recursive function to draw such a curve. You will use the methodrotateof classVector.

Figure3 – Tree construction.

It is natural to think of erasing the half of the preceding segment to draw the new branches, but it is not a great idea : the segments are discretized (in pixels), and a discretized subsegment may not go exactly on the same pixels as the master segment, hence some residual stains after erasing. The same remark will apply to the Koch curve below.

1.3 Change the implementation

7. Second implementation :

Modify the implementation of classVectorby replacing the membersdoublex,y; by an arraydoublecoord [2]; . What modifications should be applied inmain.cpp?

8. Vectors of higher dimension :

The advantage of the latest implementaion is that it can be generalized to vectors of higher dimension.

Put a global constantDIMof value 2 at the top of filevector.hand make the classVectorindependent of the dimension.

NB : The methodrotateand the accessors we defined cannot be generalized directly to higher dimension.

Leave them unchanged but reserve their use to dimension 2 by putting an assert . . .

1.4 The snowake

9. Koch curve :

This fractal curve can be built by starting from a segment and replacing the second third of each segment by two segments so as to get an equilateral triangle.

Write a recursive function to draw this curve.

Figure4 – Construction of the Koch curve.

10. Snowflake :

It is obtained by building three Koch curves from each of the sides of an equilateral triangle.

2

Références

Documents relatifs

Thus each mathematical symbol (G, L,. .) has a single definite meaning inside each exercise, but might have different meanings from an exercise to another.. It is not required to

Our research showed that an estimated quarter of a million people in Glasgow (84 000 homes) and a very conservative estimate of 10 million people nation- wide, shared our

It is the first time in the Region that a group of experts and professionals will meet to discuss the status and future of health sciences libraries in the information age and in

defined as a sequence of n (numbered 0 through n-1) arbitrary bytes to be the number n encoded as an unsigned integer (as described below), and followed by the n bytes of

The IESG performs technical review and approval of working group documents and candidates for the IETF standards track, and reviews other candidates for publication in the

Previous research work has developed tools that provide users with more effective notice and choice [9, 18, 19, 31]. With increasing concerns about privacy because of AI, some

Apart from discrete groups and duals of compact groups, the first test examples for a quantum Property RD should be the free quantum groups introduced by Wang [6], which are

2. Duty to harmonize the special or regional conventions with the basic principles of this Convention. It is a convenient flexibility to allow the amendment of the application of