• Post Reply Bookmark Topic Watch Topic
  • New Topic

Drawing Triangular Grid

 
Andrew Tran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently doing an assignment that involves Ehrenstein Illusions and I am kind of stuck on trying to draw a Triangle(the triangle on the right of the picture with 6 circles) where the squares are connected side by side. Any clues/help on how i can achieve this? Attached is a picture of what that triangular shape would look like.  Thanks in advance.

drawgrid.png
[Thumbnail for drawgrid.png]
 
Piet Souris
Rancher
Posts: 1625
35
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew,

I think you should first create a square with the circles inside, as one figure. A Path2D object seems ideal for such a figure. It also has the advantage that you can use a coordinate system that is as comfortable as possible.

For instance, create a Path2D.Double,  and add the biggest square, with coördinates (-1,-1), (1,1) et cetera,  or other coördinates you like. Next, you know the diameter of your largest circle. Add it, and go on until you are satisfied. Now you have your basic figure. Look at the Shape and Path2D api how easy it is to transform these things. What is also necessary is a decent knowledge of coordinate system when it comes to drawing these things in a panel, at the required size. See for more information the Graphics2D api.

If this all sounds too complex, you can also draw your basic figure in a BufferedImage, thát you can also rotate and collect for your big triangle

Well, my initial thoughts.
 
Campbell Ritchie
Marshal
Posts: 52518
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can draw your diamond‑and‑circle with radius r and repeat it r to the right and r up. The sides of the diamond will be r×√2.
Then you can draw your diamond‑and‑circle with radius r and repeat it r to the right and r down.
Then you can ...
 
Fred Kleinschmidt
Bartender
Posts: 451
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer depends on which triangle you are talking about. It is pretty easy to figure out the triangle that connects the centers of the outer circles.
If you want the smallest triangle that completely encloses all of the circles, then the sides of the triangle have length ( 2 * r * ( 2 + sqrt(3) ) )
 
Andrew Tran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i got them to display similar to the assignment, but I have another question.
Where would i put the g.setColor and g.fillOval in order to make the lines Black and fill the circle with Green?
Initially, i thought i would put it in the drawDisc method where i tried
g.setColor(Color.BLACK);
g.fillOval(centerX,centerY,radius,0);
g.setColor(Color.GREEN);

It seems like only the lines get changed to black, but nothing is filled into the circles.

Thanks

this is my current code:

 
Piet Souris
Rancher
Posts: 1625
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In  the code in your opening post, in the drawSubfigure-method, you have the correct sequence  already. You can use that same code in your drawDisc method., as far as I can tell.
 
Fred Kleinschmidt
Bartender
Posts: 451
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should really consider using Swing instead of AWT. Then you would create you own JPanel subclass, and do all of the drawing in its paintComponent() method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!