We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Shape composition
Page Index Toggle Pages: 1
Shape composition (Read 950 times)
Shape composition
Jul 7th, 2009, 6:30am
 
Hi there,

I'm trying to construct some simple 2D shapes, and I'm wondering what the best approach might be.   I would like to create a rectangle, however I'd like one of the 4 sides to be a curve.  I'm searched around and I can't seem to single way to do this.  Any advice?

Thanks!
Re: Shape composition
Reply #1 - Jul 7th, 2009, 7:28am
 
You'll find pretty much everything you need in the reference. The tutorials and reference pages are pretty straight forward on how to create all kinds of shapes.

Here's something to get you started. I'll admit it's quite crude, but it demonstrates what you describe above. First I make a rectangle with only three sides, then I use the bezier function to make the fourth side curved.

Code:

size(200,200); //set screen size
fill(255); //make the shape white
beginShape(); //begin building custom shape
vertex(100, 50); //specify vertices of custom shape
vertex(50, 50);
vertex(50, 100);
vertex(100, 100);
endShape(); //finish building custom shape
bezier(100, 50, 120, 70, 120, 90, 100, 100); //add curved side
Re: Shape composition
Reply #2 - Jul 7th, 2009, 5:20pm
 
Thanks very much for the reply - your code is exactly what I've been looking for.  The endShape() before the bezier still throws me off quite a bit, but I'll dig into the reference materials and try and grok it down.  Thanks again!
Re: Shape composition
Reply #3 - Jul 7th, 2009, 9:14pm
 
Well don't throw yourself off too much! It's really quite simple. Since you're drawing a custom shape (i.e. not a plain old rectangle or ellipse), the system needs to know when you're done drawing your custom stuff so it can draw the appropriate lines to connect the dots and to close it off or fill it with a color. By using beginShape() and endShape(), you're simply letting it know the boundaries of your custom shape and that you're done drawing vertices. Using rect() or bezier() does the same exact thing underneath, but Processing hides that stuff from you so you don't have to write out all those points every time you simply want to draw a rectangle. Keep practicing, and it'll all eventually make sense!
 Wink
Re: Shape composition
Reply #4 - Jul 7th, 2009, 11:05pm
 
Henry P wrote on Jul 7th, 2009, 5:20pm:
The endShape() before the bezier still throws me off quite a bit
Indeed. But you can also use bezierVertex() inside the shape.

PS: forum acted up, you have duplicates of your messages, please go to each extra message and delete it from there. Thanks!
Re: Shape composition
Reply #5 - Jul 9th, 2009, 1:33pm
 
So it looks like I'm simply creating two filled shapes in the example, no?  So how would I create a filled shape where curved side is an S curve?
Code:
size(200,200); //set screen size
fill(255); //make the shape white
beginShape(); //begin building custom shape
vertex(100, 50); //specify vertices of custom shape
vertex(50, 50);
vertex(50, 100);
vertex(100, 100);
endShape(); //finish building custom shape
bezier(100, 50, 50, 50, 150, 100, 100, 100); //add S curved side





Re: Shape composition
Reply #6 - Jul 9th, 2009, 2:01pm
 
An interesting question.  At first I was distracted by your bezier call after the endShape; but as PhiLho suggested bezierVertex is the answer:

Code:
void setup() {
background(200);
size(200,200); //set screen size
stroke(0);
fill(255); //make the shape white
beginShape();
vertex(100, 100);
vertex(50, 100);
vertex(50, 50);
vertex(100, 50);
bezierVertex(100, 50, 65, 50, 100, 75);
bezierVertex(100, 75, 135, 100, 100, 100);
endShape();
}
Page Index Toggle Pages: 1