Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Koch Snowflake

Koch Snowflake

Scheduled Pinned Locked Moved Java
questionjavagraphicshelp
2 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vultron_3229
    wrote on last edited by
    #1

    Hey folks, I have a question about writing the koch snowflake in java. When I compile my code I only get the snowflake on one side(so im guessing this is the koch curve instead) and I was wondering how do I get it to all the sides in order to form the snowflake? Here is my code, I know I am missing something but i'm not so sure what. thanks for the help: import javax.swing.*; import java.awt.*; public class drawSnowflake extends JApplet { int level=0; public void init() { String levelStr= JOptionPane.showInputDialog("Enter the recursion depth: "); level = Integer.parseInt(levelStr); } public void paint(Graphics g) { int pointOne = 60; int pointTwo = 120; int pointThree = 160; int pointFour = 140; drawKochSide(g, level, pointOne, pointTwo, pointThree, pointFour); } private void drawKochSide(Graphics g,int lev, int x1, int y1, int x5, int y5) { int deltaX, deltaY, x2, x3, x4, y2,y3, y4; if (lev<1) g.drawLine(x1, y1, x5, y5); else{ deltaX = x5-x1; deltaY = y5-y1; x2 = x1 + deltaX / 3; y2 = y1+ deltaY / 3 ; x3=(int) ((x1+x5)/2 + Math.sqrt(3.0)*(y1-y5)/6); y3=(int) ((y1+y5)/2+ Math.sqrt(3.0)* (x5-x1)/6); x4 = x1 + 2 * deltaX / 3 ; y4 = y1 + 2 * deltaY / 3 ; drawKochSide(g, lev-1, x1, y1, x2, y2); drawKochSide(g, lev-1, x2, y2, x3, y3); drawKochSide(g, lev-1, x3, y3, x4, y4); drawKochSide(g, lev-1, x4, y4, x5, y5); } } }

    D 1 Reply Last reply
    0
    • V vultron_3229

      Hey folks, I have a question about writing the koch snowflake in java. When I compile my code I only get the snowflake on one side(so im guessing this is the koch curve instead) and I was wondering how do I get it to all the sides in order to form the snowflake? Here is my code, I know I am missing something but i'm not so sure what. thanks for the help: import javax.swing.*; import java.awt.*; public class drawSnowflake extends JApplet { int level=0; public void init() { String levelStr= JOptionPane.showInputDialog("Enter the recursion depth: "); level = Integer.parseInt(levelStr); } public void paint(Graphics g) { int pointOne = 60; int pointTwo = 120; int pointThree = 160; int pointFour = 140; drawKochSide(g, level, pointOne, pointTwo, pointThree, pointFour); } private void drawKochSide(Graphics g,int lev, int x1, int y1, int x5, int y5) { int deltaX, deltaY, x2, x3, x4, y2,y3, y4; if (lev<1) g.drawLine(x1, y1, x5, y5); else{ deltaX = x5-x1; deltaY = y5-y1; x2 = x1 + deltaX / 3; y2 = y1+ deltaY / 3 ; x3=(int) ((x1+x5)/2 + Math.sqrt(3.0)*(y1-y5)/6); y3=(int) ((y1+y5)/2+ Math.sqrt(3.0)* (x5-x1)/6); x4 = x1 + 2 * deltaX / 3 ; y4 = y1 + 2 * deltaY / 3 ; drawKochSide(g, lev-1, x1, y1, x2, y2); drawKochSide(g, lev-1, x2, y2, x3, y3); drawKochSide(g, lev-1, x3, y3, x4, y4); drawKochSide(g, lev-1, x4, y4, x5, y5); } } }

      D Offline
      D Offline
      douss
      wrote on last edited by
      #2

      Hi vultron, I don't know if you really understand your code. In your code, you clearly defined two points (only two), let's say point A and point B. Coordinates of point A are given by :

      int pointOne = 60;
      int pointTwo = 120;

      Coordinates of point B are given by :

      int pointThree = 160;
      int pointFour = 140;

      Then you draw one side of the snowflake (the side A-B) by :

      drawKochSide(g, level, pointOne, pointTwo, pointThree, pointFour);

      What you have to do is to define a third point C and to draw the two other sides (B-C) and (A-C), so there's the final code of the paint method :

      public void paint(Graphics g)
      {
      int pointOne = 50; //x coordinate of point A
      int pointTwo = 100; //y coordinate of point A
      int pointThree = 150; //x coordinate of point B
      int pointFour = 100; //y coordinate of point B
      int pointFive = 100; //x coordinate of point C
      int pointSix = 14; //y coordinate of point C

      drawKochSide(g, level, pointOne, pointTwo, pointThree, pointFour);
      drawKochSide(g, level, pointFive, pointSix, pointOne, pointTwo);
      drawKochSide(g, level, pointThree, pointFour, pointFive, pointSix);

      }

      Notice that in this example, the triangle ABC is not exactly equilateral because coordinates are integers.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups