Exercises in this lecture   Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home   

Exercise solution 3.5
Tegning af en regulær polygon


Mit program, som løser problemet, er følgende:

import oopcourse.util.*;
import java.awt.*;
import cs1.*;

class CrayonPolygon {

  static Crayon cb = new Crayon(Color.red, 3);

  public final static double EDGE_LENGTH = 50;

  public static void drawPolygon(Crayon c, int degree){
    double angel = 360/degree;
    for(int i = 1; i <= degree; i++){
      cb.move(EDGE_LENGTH);
      cb.turn(angel);
    }
  }

  public static void main(String[] args){
    System.out.print("Hvormange sider i polygonen:");
    int n = Keyboard.readInt();
    cb.jumpto(200,200);
    drawPolygon(cb, n);
  }
}

Her er et link til programmet.