Adding more vertices
There are many flags that contain stars that just cannot be created by overlapping triangles. In this recipe, we will figure out how to create a star that contains an arbitrary number of points We will use the same key concept we discovered in the previous lesson by taking advantage of a virtual circle to calculate positions, this time with two virtual circles. In this lesson , we will create the flag of Somalia and in the process figure out how to create a function that will enable us to create stars.
Keynotes:
- Creating a multi pointed shape
- It's all about stars now.
- Using two virtual circles to create a shape.
Video Transcript
00:00In the last lecture, we created the Star of David,
00:03the most complicated shape we created so far.
00:06And in the same tradition,
00:07we're gonna make our shapes even more complicated
00:10by adding more vertices to our star,
00:12making a star that could have an endless amount of points.
00:16How do you create a star of that shape?
00:18And really the process of that is, well,
00:21to create this multi-pointed shape
00:23is we're gonna need to think now
00:25about how a star is constructed.
00:27And when you think about it,
00:28there are actually two circles,
00:30an inner circle and an outer circle.
00:32We're gonna really take a look at
00:34how could we create the zigzag lines
00:36between the inner circle and outer circle
00:38to create a star with as many points
00:40as we would want it to have.
00:42We're gonna use this virtual circles to create a star.
00:46It's a really fascinating lecture.
00:47So let's jump right into it.
00:50It's time for us to create
00:52our most complicated creation to date.
00:55Now, so far we worked with, for example,
00:58the flag of Israel, the most complicated shape
01:00where we had two shapes that we intertwined together
01:03to create a pseudo star, really.
01:05The next step is really to create
01:07an endless amount of points to our star.
01:11Now, to do that, I'm gonna go ahead
01:12and actually create the flag of Somalia.
01:15So let's go ahead and start doing that.
01:16Let's go right into our code
01:18and let's start building the flag of Somalia.
01:20Now, I went ahead and advanced
01:22and already created the initial starter function.
01:25So make sure that you do that before you play the video,
01:28same starter that we've been using all along.
01:31All right, so the first step that we do
01:32after we create the context itself
01:34is we wanna fill a rectangle
01:37and wanna configure its style to be that blue,
01:40that beautiful, really beautiful blue that Somalia has.
01:43And I'm gonna go ahead here and set that color
01:45that I pre-configured initially.
01:47And I'm gonna go ahead and fill that rectangle,
01:50setting it to take the full width
01:52and height of our screen.
01:53So far, same old.
01:56The next step that we're gonna do
01:57is we're gonna create a helper function
01:59and this helper function will enable us to create stars.
02:03Now, I'm gonna walk you through the elements of the star.
02:07I'm gonna call this create star.
02:09Now, the create star function
02:10is gonna get a context as its first property.
02:13The second property is gonna be the X and Y coordinates
02:15and as always, we're gonna send in that center point
02:18for our X and Y coordinates.
02:20The next coordinate is gonna be a small radian
02:23and a large radian.
02:24I'm gonna go ahead and just initially put here 10
02:27and I'm gonna put here 40.
02:28The idea is we'll have a small radius, not radian, sorry,
02:31a small radius and a large radius.
02:35Once we do that, we also wanna define
02:37how many points will our star have.
02:40I'm gonna say that it has 20 points.
02:43Next property that we'll wanna define
02:45is the color of our star.
02:47Now, I wanna make sure that we're gonna have here
02:49an option to do a fill or a stroke.
02:53I'm gonna set the first property to be a fill
02:57and the next property, I'm gonna set it to be a stroke.
03:00Now, technically, we don't need a stroke,
03:02so I'm gonna send in nothing
03:04and last but not least,
03:05I'm gonna put here another last property
03:07and this property will enable us to tilt by degrees,
03:11a number between zero and 360,
03:14to tilt our shape that we're creating.
03:16All right, so those are the properties of create star
03:19and I'm gonna go ahead and actually create that method.
03:22Let's go ahead here and create that steps.
03:24Create star.
03:25First, we said it's gonna be the context.
03:27Second, we said it will be x and then the y
03:29and then we'll have the inner radius and the outer radius
03:33and then we're going to have how many points we have.
03:37Let me see what else are we missing here.
03:39Once we have the points, we'll have a fill color,
03:42then we'll have a stroke color
03:46and then we'll have a tilt.
03:48All right, so we have here quite a lot of properties
03:50and we're gonna take advantage of all of them.
03:52We're gonna use all of them.
03:54Let's go ahead and create some helper variables
03:56and I'm gonna go ahead starting with degree
03:58because we always need a degree
04:00and I'm just gonna divide that and get my degree.
04:03The next item that I'm gonna need
04:05and I'm just gonna walk you through the logic here
04:07for a second.
04:08I wanna take the amount of star points,
04:10which is one, two, three, four, five.
04:13When I'm getting those five, really what I wanna do
04:15is I wanna basically calculate the size
04:17that we have to take each step.
04:20So basically, the idea behind here
04:23is to basically figure out what is the size of each step.
04:26Now, because we wanna have a step to one direction
04:29and then a step back,
04:31we wanna basically take,
04:32basically we're traveling one, two, three, four.
04:35So if there's, basically if there's five points,
04:37we're traveling 10 lines.
04:39All right, so let's go ahead and create this
04:40and it'll make sense once I start creating it.
04:43So our step itself, how large is each step?
04:46Each step is basically 360 degrees
04:49divided by the amount of points,
04:50the amount of points the item has.
04:52Beyond that, we also wanna divide that by two
04:54because for each point, there's gonna be two steps.
04:58Last but not least, we also wanna change the degree
05:00and the angle that we're in every time.
05:03So I'm basically moving my angle.
05:05If we look at this, I'm basically moving my angle
05:08each time the angle is changing
05:10because really when we're moving each time,
05:12we're in a different angle.
05:13So if we have your literally one, two, three, four, five,
05:17six, seven, if we have, why am I counting again?
05:19If we have five points,
05:21that means we have 10 dots where we're gonna draw in, right?
05:25Okay, and the gap and the distance between them is equal.
05:28So I'm literally creating the step
05:30that was the size of half of the distance
05:32between this point and that point, okay?
05:35All right, in a circle sphere, in a sphere itself, right?
05:39Inside of our degrees and radians.
05:41Okay, and I'm gonna go ahead and also save
05:43the current step that we're in
05:46because we're definitely gonna need to use it
05:47as we're progressing and adding steps into it.
05:50We're gonna need a reference to it.
05:52And last but not least,
05:53we also wanna calculate the radian, that tilt,
05:56so I'm gonna call it radian tilt,
05:58which is basically just gonna be the degree itself
06:00or one radian, basically out of 360, that portion,
06:06times our tilt, making sure that if there is a tilt
06:09that's being sent,
06:10then we'll be able to gather that into it as well.
06:13That's all the helper variables
06:15that we're gonna need, quite a lot of them.
06:17But let's go ahead and start building it.
06:18So first step is in the creation process itself
06:22of the drawing is we're gonna begin a path,
06:25we're gonna go ahead and also close that path,
06:30and let's go ahead and even go through the steps.
06:32So first of all, if there is a fill color,
06:34what do we wanna do?
06:36In the case that there's a fill color,
06:38I'm gonna go ahead and actually fill it out
06:40by defining the context that fills style
06:44and setting it to be the fill color.
06:47Okay, then I'm gonna go to my context
06:48and I'm gonna fill out the shape that we've just created.
06:51Beautiful, let's do the exact same thing for stroke.
06:55If the fill, if the stroke color,
06:59if the stroke color is there,
07:02then we're gonna set the stroke color
07:04and we're gonna put there the stroke style.
07:08And last but not least,
07:10we're gonna call that stroke method as well.
07:12All right, so we have the construct.
07:14All that's left for us to do now
07:15before we could actually see anything
07:17is actually create the actual steps,
07:18which is probably what you're waiting for,
07:20the most complicated part here.
07:22So to do that, I'm gonna go ahead and create a new loop.
07:25And this loop is literally gonna walk through all the points
07:29and travel through them point by point.
07:32What do we wanna do in each point?
07:33In each point, what we actually wanna do is two steps.
07:36Remember, we have your division of two for each point.
07:38So let's start with step one.
07:40Step one, we're gonna go ahead.
07:42First of all, the first thing we're gonna do
07:44is we're gonna go to our current step
07:46and we're gonna put in that first step position.
07:50The next step is we're gonna go ahead
07:52and we're gonna start drawing.
07:54So I'm gonna line two.
07:56And where do I wanna line two?
07:57Well, is it a little bit complicated?
07:59Let's break a line here so it will be easy for us to see.
08:01First of all, we have our X and Y coordinates.
08:03That's given.
08:04Beyond that, we also wanna make sure
08:06that we're going to that math, sine, and cosine.
08:08So let's just put that in
08:10because we already know that that's coming, right?
08:13And for our Y, we're gonna have there a math.
08:16Cosine.
08:19And we know, Otto, that we really, what we wanna do
08:22is we wanna travel to the inner circle
08:25and then to the outer circle.
08:26And because we're starting really from the inner circle,
08:29from technically speaking,
08:31so I'm gonna go ahead and basically duplicate
08:34by our external radius.
08:36So I'm gonna do external radius.
08:39We're going out to our external radius.
08:42So that's really the step,
08:43but now we just have to put inside of here
08:44the radian that we're going into,
08:47which is literally the radian that we've just calculated,
08:48the current radian.
08:50Last but not least, which we'll take there that soon,
08:52which we'll be adding that tilt,
08:54but let's forget about the tilt for a second.
08:58Not titled, this should be tilt.
09:01Tilt.
09:03Okay.
09:04Tilt.
09:05There we go.
09:06So first of all, that's the line.
09:07We make our line, we're going with our line
09:09to that external circle point.
09:12Once we got to the external circle point,
09:14what's the next step?
09:14What we actually wanna do,
09:15we wanna do the exact same thing.
09:18We wanna step further in our step,
09:20but contrary to before that we were going
09:23to our external item,
09:24now we wanna go into our internal radius.
09:27Now, this is literally everything I had to do
09:29to make this work.
09:31There's only one more small little manipulations
09:34that we probably wanna put into this.
09:36But let's go ahead and just see this
09:37if everything is working, if we don't have any error.
09:39So first of all, we see we have here a star
09:41that has a lot of points.
09:43So it's working, working really nicely.
09:45But let's go ahead and also add,
09:46let's change the amount of points.
09:48Let's make it the real amount of points,
09:49which is actually five points.
09:51So I'm gonna go ahead there and save that,
09:53click on refresh, and now we have here five points.
09:56Now, before we even configure the real sizes here,
09:59I wanna see that tilting really works,
10:01which it won't right now.
10:02I wanna send here, for example, a tilt of 20 degrees,
10:06or even let's do 30 degrees.
10:08When I create that tilt, it's not gonna work right now,
10:10so let's add it in.
10:11What I wanna do is I wanna take that radian tilt variable
10:14that we configured and just add it into all the places
10:17where I'm actually drawing things out.
10:20So I'm literally just adding there that tilt
10:22that usually will equal nothing, or zero,
10:25and I'm adding it in for cases where it isn't.
10:29And that's all I had to do to add the tilt inside.
10:32So we're almost done with this flag.
10:34Now we can also tilt it and you could animate it.
10:36It could be really cool to see these stars animating.
10:38It could be very easy for you to do now
10:40that we have this logic set in place.
10:42All that's left for me to do,
10:43I'm gonna go ahead here and set this to be undefined,
10:46or zero more accurately, and now I'm gonna go
10:48and figure out what would be the right value
10:51to put in for our radians.
10:52And again, I want this to be relative items.
10:55So I'm gonna go to my height,
10:56because that is gonna be the most relative value
10:59that I could depend on, always to be the same.
11:01And I already pre-configured that for our small position,
11:04I want basically 13th of the height,
11:06and for our larger circle,
11:08I want it to be about a fifth of the site.
11:10That's really a calculation that I did
11:12when I was playing around with it.
11:14And that's it, once I've done that,
11:15I have the flag, and not only do I have a flag,
11:19it is the flag of Somalia,
11:20which is really beautiful, to be honest.
11:23So that's it, we're done creating another flag
11:25and another really complicated construct.
11:28And literally, you could create with this contract,
11:30even before I wrap this,
11:31you could create even more complex shapes.
11:33Because if I go here and I switch this around,
11:36look what will happen.
11:38If I switch this around, I could go around inside of here
11:41and create a shape that would be very interesting.
11:43The exact opposite, or it's flipping around.
11:46More over than that, if I went ahead
11:47and decided that I want really, really small differences,
11:51I could create a very interesting shape
11:53that wouldn't even look like a star,
11:54but it would be from the family of stars, you could say.
11:57And I could create really interesting shapes this way.
12:02Okay, so play around with the logic here,
12:03because you could create a lot of stuff
12:05with this create star logic,
12:07a lot of really beautiful shapes.
12:08So that's it.
12:10In this lecture, we really doubled up on our knowledge.
12:12We learned how to use two virtual circles
12:15to create a really complicated shape, a star.
12:19In the next lecture, we're gonna go ahead
12:20and look at how do we create even more complicated shape
12:24using very simple techniques,
12:26such as overlapping shapes to create other shapes.
12:29Let's look at that in the next lecture.
