• Aucun résultat trouvé

Interpolation/Extrapolation

Dans le document Using the Processing Language (Page 195-198)

Advanced Graphics Algorithms

7.4 Interpolation/Extrapolation

Hybridization (a.k.a. morphing) is a procedure in which an object changes its form gradually in order to obtain another form. Morphing is a gradual transition that results in a marked change in the form’s appearance, character, condition, or func-tion. The operation of morphing consists basically of the selection of two objects and the assignment of a number of in-between transitional steps. The first object then transforms into the second in steps. The essence of such a transformation is not so much in the destination form but rather in the intermediate phases this type of transformation passes through, as well as, in the extrapolations, which go beyond the final form. It is the transitional continuity of a form that progresses through a series of evolutionary stages.

Morphing can be seen as either an image or a geometrical transformation.

Geometrical morphing preserves the structural integrity of the objects involved, that is, an object changes into another object as a single entity. A cube, for instance, may be gradually transformed into a pyramid. From the viewer’s point of view, there are always two objects: the original (or source), to which transfor-mation is applied, and the destination object (or target), which is the object one will get at the final step of the transformation. However, theoretically, there is only one object, which is transformed from one state (original) into another (destination). This object combines characteristics of both parent objects, which are involved in the transformation, and is called a hybrid object. This object is actually composed of the topology of the one object and the geometry of the other. It is an object in disguise. Although it is topologically identical to one parent, it resembles the geometry of the other parent.

Interpolation is a method for estimating values that lie between two known values.1 The hybrid object derives its structure from its parents through for-mal interpolations. While it is easy to derive hybrid children from isomor-phic parents, a challenge arises for heteromorisomor-phic parents. In an isomorisomor-phic transformation, a one-to-one correspondence applies between the elements of the two parent sets, such that the result of an operation on elements of one set corresponds to the result of the analogous operation on their images in the other set. In the case of heteromorphism, the lack of homogeneity between the parents leads necessarily to a selective process of omission and inclusion of elements between the two sets. The guiding principle in this mapping process is the preservation of the topological and geometrical properties of the hybrid object. For instance, in the case of a square mapped to a triangle, the addition of a fourth point to the triangle preserves the topology of the square, and yet its disguised location preserves the geometrical appearance of the triangle.

In the following example, a square is mapped to a triangle: the hybrid child is a four-sided polygon in which two of the vertices overlap and these vertices are ordered to form a triangle. The problem here is to map two counters so that,

when the one is counting points from one object to another, counter kc should skip points from the other object. For example, if the counter k1 increments as 01234 the counter k2 should increment as 00123 (or 01123 or 01223 or 01233). To obtain such behavior, we use the formula kc = k/(p1/p2) or kc = k/(p2/p1).

1 float [] x1 = {200,100,100,200,200}; //parent1 2 float [] y1 = {200,200,300,300,200};

3 float [] x2 = {350,300,400,350}; //parent2 4 float [] y2 = {200,300,300,200};

5 float [] xc, yc; //child

6 float ratio=0.5; //percentage of interpolation

7 int k1, k2, maxpoints; //number of points for the arrays 8 void setup(){

9 size(500,400);

10 smooth(); //for visual effect

11 maxpoints = max(x1.length, x2.length); //the max number of either array

12 xc = new float[maxpoints]; //create a child with points as the largest parent

13 yc = new float[maxpoints];

14 }

15 void draw(){

16 background(255);

17 stroke(0);

18 for(int i=1; i<xc.length; i++) //draw the child’s lines 19 line(xc[i],yc[i],xc[i-1],yc[i-1]);

20 for(int i=0; i<xc.length; i++) //draw the child’s vertices 21 rect(xc[i]-1,yc[i]-1,3,3);

22 stroke(255,0,0);

23 for(int i=1; i<x1.length; i++) //draw parent 1 24 line(x1[i],y1[i],x1[i-1],y1[i-1]);

25 for(int i=1; i<x2.length; i++) //draw parent 2 26 line(x2[i],y2[i],x2[i-1],y2[i-1]);

27 } 28

29 void mouseDragged(){

30 for(int k=0; k<maxpoints; k++){

31 if(x1.length>=x2.length){ //if p1 is greater than p2 32 k1 = k; //counter 1 remains as is

In the first five lines of the code, we declare six arrays to hold the coordinates of two parents and the child. We define a ratio of interpolation as 0.5 (that is half the distance of the path). Then we define three variables to hold the number of points of the parents and the child. In the setup() section, we increase the array xc[] and yc[] that will hold the child’s points to the length of the larg-est (in number of points) parent. This is done because we know that the child will hold the number of the largest parent but the coordinate locations of the smallest parent.

In the draw() section, we simply draw the child’s lines (stored in arrays xc[]

and yc[]) and also the vertices as little rectangles (for visual purposes). Then we draw the two parents.

In the mouseDragged() section, we loop through all points of the child and distinguish two possibilities:

1. If parent 1 is greater than parent 2 (i.e., has more points than parent 2), then the counter k1 remains as is, but the counter k2 (which will collect points for the smaller parent) is modified according to the formula described earlier.

2. If parent 2 is greater than parent 1 (i.e., has more points than parent 1), then the counter k2 remains as is, but the counter k1 (which will collect points for the smaller parent) is modified according to the formula described earlier.

The result of this algorithm is shown in Figure 7-8 and 7-9.

Figure 7-8: Interpolation of a square into a triangle in six steps

Figure 7-9: Interpolation of a square to a triangle, halfway (above), and multiple steps of interpolation and extrapolation of a square into a triangle and beyond (below)

Dans le document Using the Processing Language (Page 195-198)