TP1: indices

Modification 1 : Sélection en rectangle

- rajouter à la classe MyShape une méthode

    boolean isShapeInsideRectangle( AlignedRectangle2D r /* in world space */ ) {

        if ( type == POLYGON ) {

            ...
        }

        else if ( type == CIRCLE ) {
            ...

        }
    }

- rajouter dans MyCanvas
        boolean isSelectionRectangleBeingDragged

- chercher dans SimplePaint.java les endroits où on utilise
    isHilitedShapeBeingMoved
  et
    TOOL_SELECT_AND_MOVE

- rajouter dans MyCanvas.paintComponent() du code pour dessiner le rectangle

    if ( isSelectionRectangleBeingDragged ) {

        gw.setColor( ... );
        gw.drawRectangle( ... );

    }

- Attention de bien convertir entre les systèmes de coordonnées monde et pixel

- Rajouter à MyShape

    boolean isSelected

- s'arranger pour que le MyShape.isSelected soit mis à vrai/faux au relâchement pour chaque forme à l'intérieur/extérieur du rectangle

- modifier MyCanvas.paintComponent() :

        for ( int i = 0; i < shapes.size(); ++i ) {
            MyShape shape = shapes.get(i);
            shape.draw( gw, true, i == currentlyHilitedShape || shape.isSelected );
        }

- modifier mouseDragged() :

    if ( isHilitedShapeBeingMoved && currentlyHilitedShape > -1 ) {
        ...
        rajouter une boucle pour déplacer toutes les formes sélectionnées

    }

- etc. (voir l'énoncé)


Modification 2 : Outil de crayon

Rajouter à la classe MyShape

    public static final int POLYGON = 0;
    public static final int CIRCLE = 1;
    public static final int POLYLINE = 2; // ligne brisée

Rajouter à la classe MyShape

    // returns distance in world space units
    float distanceFromShapeToPoint( Point2D p /* in world space */ ) {

        if ( isPointInsideShape( p ) ) return 0;

        if ( type == POLYLINE ) {
            float minDistance = points.get(0).distance( p );
            for ( Point2D q : points ) {
                float distance = q.distance( p );
                if ( distance < minDistance )
                    minDistance = distance;
            }
            return minDistance;

        }
        return 1000000;
       
    }

- changer indexOfShapeUnderPixel() pour appeler distanceFromShapeToPoint() ...

    // returns -1 if no shape is under the mouse cursor
    private int indexOfShapeUnderPixel( int x, int y ) {
        for ( int i = shapes.size()-1; i >= 0; --i ) {
            MyShape shape = shapes.get(i);
            if ( shape.isPointInsideShape(
                gw.convertPixelsToWorldSpaceUnits( new Point2D(x,y) )
            ) ) {
                return i;
            }
        }
       for ( int i = shapes.size()-1; i >= 0; --i ) {
            MyShape shape = shapes.get(i);
            if ( shape.distanceFromShapeToPoint(
                gw.convertPixelsToWorldSpaceUnits( new Point2D(x,y) )
            ) < 10 ... gw.getScaleFactorInWorldSpaceUnitsPerPixel() ) {
                return i;
            }
       }
        // no shape was found to be under the mouse
        return -1;
    }