Creating Complex Shapes
It's time to take everything we learned and integrate it into the most complex shape we have seen so far, the Star of David. This star is part of the flag of Israel and to create it we will need to use our triangle logic, sin and cosine.
Keynotes:
- Creating complex shapes with circles.
- Layering shapes on top of each other.
- sin and cos in action.
Video Transcript
00:00Taking advantage of the sine, cosine, and radians,
00:04we're gonna create more complex shapes.
00:06In this lesson, we're actually gonna create
00:08the star of David that is a construction of two shapes.
00:11We're gonna create complex shapes with circles
00:14because we need the help of that circle radians
00:17to create a perfect triangle that could sit in the center
00:20and we could overlap another circle,
00:22another shape on top of it.
00:24So we're gonna be layering two triangles together
00:26to create the star of David.
00:28And we're gonna obviously use to do that sine
00:31and cosine and radians to be able to position the items
00:34exactly, exactly in the right spot.
00:37So let's go ahead and create the flag of Israel
00:40and the star of David for that matter.
00:43All right, let's continue and create our next flag.
00:46And the next flag is gonna be the flag of Israel.
00:49And if I bring it here into the stage,
00:51it is probably the most, not probably,
00:52it's the most complicated shape we've created until now.
00:56Now, the shape although looks like just a triangle,
00:59it's really hard to figure out exactly how to position it.
01:02Now, lucky for us in our last lesson
01:04when we learned about how to create a clock
01:07and covered the cosine and sine,
01:09that gave us everything we needed to be able to configure
01:12and create that type of complex shape.
01:15So let's go ahead and we're gonna,
01:16what we're gonna do and I went ahead already
01:18and started creating the create Israel section here.
01:21So make sure that you follow along,
01:23just go ahead and add into your on canvas ready area,
01:28which was really, really far early long ago
01:30on due canvas of around line 24.
01:33Just make sure that you're instantiating the create Israel.
01:37And I made sure that if I go ahead
01:40and go back into my create Israel,
01:43we'll see that I already started configuring it
01:46with a width, a height, the context,
01:47the X and Y coordinates,
01:49which we've been working with so far
01:51almost in every single one of our elements.
01:53Now the way we're gonna create the triangle,
01:56and if we take a really quick look again at this triangle,
01:59two triangles, one on top of each other,
02:02I'm gonna imagine that there's a circle
02:04bounding this triangle.
02:06And by creating the circle, invisible circle around it,
02:09I could go to specific points on the circle,
02:12AKA one, two, three to create a triangle,
02:16one, two, three to create a triangle.
02:18And literally what I'm gonna do
02:19is I'm gonna use my fill triangle quickly to create it.
02:23So let's go ahead and do that.
02:24So to do that, I'm gonna go ahead and define a radius.
02:26And I was playing around with the numbers,
02:29more or less the right value would be
02:31the height of our page divided by 4.7 for our radius.
02:37Now I'm gonna go ahead and also create a reference for pi,
02:41which is gonna be the math.pi,
02:42just a shortcut reference
02:44because we're gonna need it to work with it.
02:46And also I'm gonna create a degree or a radian,
02:49which is basically our pi divided by 180, beautiful.
02:54So now we have quite a lot of helper variables.
02:56There's gonna be probably a few more down the path,
02:59but let's go ahead and start creating,
03:00starting with creating a filled rectangle
03:03that would take our full screen with an height.
03:06But before we do that,
03:07we wanna also define a background color.
03:09So I'm gonna go in my context fill style
03:11and set it to be white.
03:14Okay, so that we were gonna have a nice blank white sheet.
03:17If I go ahead here and just click on refresh,
03:19our flag should now be a nice empty, whoopsie,
03:22we actually wanted it to be white.
03:24So one, two, three, four, five.
03:27Let's click there on refresh.
03:29There we go.
03:30So we have ourselves a nice beautiful white empty sheet.
03:34Next step that we wanna do is create that triangle.
03:37And I'm gonna go ahead and use our fill triangle
03:41to start us off with.
03:42So first of all, what do we wanna do?
03:44We wanna fill out a triangle.
03:46We're gonna send in the context.
03:48Okay, that's the given.
03:49What is our first x, y coordinates?
03:52Let's start with just defining
03:54the core basic triangle points,
03:57which is basically we have our center point,
03:59which is x and y, right?
04:01Beyond that x and y coordinates,
04:03what we really wanna do is we wanna add a math sign
04:08for coordinate zero and duplicate it by our radius.
04:12And I'm gonna do the exact same thing for the y
04:15only instead of cosine,
04:16instead of sine, we're gonna use cosine.
04:19Okay, so let's do that.
04:21We're gonna use the cosine here.
04:23And let me just break a line here so we could see this.
04:25Our x coordinate is gonna be basically the center point.
04:28We're taking the radius
04:29and we're asking to get position zero
04:32in our reference point using our sine and cosine.
04:36All right, so the next property that we wanna go
04:38is we basically wanna push along as we're going along
04:42from zero, where do we wanna move to after?
04:44If we have a triangle, a circle, which is 360 degrees,
04:48really we wanna go to position zero, 120 and 240.
04:54So what I wanna do is in the next edge,
04:56I wanna go to my degree and duplicate it by 120.
04:59So that would give us the next point in our,
05:03if I divide the 360 degrees equally,
05:07then it would go to zero, 120 and 240.
05:10So let me go ahead here and just very quickly
05:12also assign the 240 and we're duplicating again
05:16because sine and cosine accepts radians
05:19and not actually degrees.
05:21So I'm gonna go ahead here and put your 240.
05:23Last but not least, I also wanna send in a color
05:25and I already have that pre-configured,
05:27which is our blue that would match the flag of Israel.
05:30And if I go ahead here and save this,
05:32let's go ahead and just see how this triangle looks like.
05:35And if I go ahead and click on refresh,
05:37our triangle should be a beautiful blue triangle.
05:40Let's go ahead and continue.
05:42The next is we wanna create the exact same triangle
05:44only tilted.
05:46So how much do we wanna tilt this by for this to work?
05:49Well, we wanna tilt it by 180 degrees
05:52because we want exactly the opposite
05:53of what we've just created.
05:55So let's go ahead and also add here a reference
05:59for our pie, which we already have.
06:00And what we wanna do is we wanna add our pie,
06:03which is 180 degrees.
06:04We wanna create another rectangle,
06:07but this time around, we wanna send in,
06:09instead of zero and 120,
06:11we wanna basically add the length of basically
06:14a half a circle into every single one of our elements.
06:17So I'm going around here
06:19and literally just assigning that value
06:22to be added into all of our points,
06:26basically flipping everything around
06:28to be completely in the other direction.
06:31If I go ahead here and I click on refresh,
06:33we'll see that now we're gonna have the full triangle.
06:35Now, obviously this is not the flag of Israel yet
06:37because this triangle is opaque
06:40and for the flag of Israel,
06:41we need it to be actually just lines
06:44and not actually circles and not actually fills.
06:46So to do that, I'm gonna go ahead into our fill triangle
06:51and I'm just gonna duplicate it
06:52and recreate it,
06:53literally changing only a couple of small things.
06:56First of all, I'm gonna call this stroke triangle
07:02and I'm gonna call the stroke command.
07:06And last but not least,
07:07I'm gonna change it to be stroke style.
07:12So that's it, I've just changed that method.
07:14Now, obviously there's a lot of duplication going on here
07:17and I'll leave it to you to clean things up,
07:19but I'm gonna go into my triangles
07:21and literally just call the stroke triangle
07:23instead of the fill triangle.
07:25And if everything was done appropriately,
07:27we'll have now a nicely filled triangle.
07:30All that's left for me to do is also define
07:32how thick will be my stroke.
07:35So I'm gonna go ahead and also add a line with,
07:38so I'm gonna go to my context
07:41and I'm gonna set a line with
07:44and I already pre-calculated
07:45that it would be the radius divided by 5.5
07:48and again, I'm using relative numbers
07:50because I want everything to resize really nicely
07:53as you're resizing it
07:54and changing the size of your element
07:57and just play around with the values
07:58until you get a value that you feel comfortable with.
08:01All that's left for us to do
08:02is to draw our two extra lines here.
08:04So right underneath the creation of our triangles,
08:07I'm gonna go ahead and continue that process.
08:10I'm gonna go ahead to my context
08:12and I'm gonna set a new line with
08:14and this line with really what I'm gonna use that
08:17a lot of times as I'm drawing things out.
08:20So even before I do that,
08:21I'm just gonna go ahead here and just add that line with
08:24and let's figure out how wide do we want it to be.
08:26And again, I played around with the values already
08:29and it would be basically roughly
08:32our radius divided by 1.4
08:34and I was literally just playing with the values
08:36until it just felt right.
08:38Okay, so now that we have our line with,
08:40I'm just gonna set it
08:41and it's time for us to go ahead
08:42and start drawing things out.
08:45So the first step is what we wanna do
08:47is we wanna begin a new path
08:48because we wanna make sure that everything that was
08:51won't be touched or changed with.
08:53Next thing, I'm gonna move my line
08:55to where do I wanna move it?
08:56First to the zero position
08:58and I also wanna account the line with.
09:00Now, because the line itself contrary to a fill
09:04grows in both directions,
09:05if I set it to be the line with itself,
09:08what I'm actually doing is I'm creating a gap
09:10of a half of the line with on the top area.
09:14Let me show you what I mean.
09:16So if I go ahead here and just really quickly
09:18create this line to
09:21and I'm gonna go to the with itself
09:23keeping my line with in the same position,
09:25let's just go ahead here
09:26and let's stroke this out for a second
09:30and run it in our application.
09:32You'll see that our line itself
09:34is basically offset by the half of its width
09:37and that's because I pushed it one size down
09:40because really our coordinate is exactly in the center here.
09:43If I went ahead and I just set here the value to be zero
09:47all the way in the top,
09:48I would actually only get half of the color.
09:50Literally, I would only get this white area colored in blue
09:53and that is because the other half would be outside
09:56of the screen area, okay?
09:58So let's go ahead and create our second line
10:01which will be more or less the exact same thing.
10:03We're only gonna change our actual height position
10:06and we wanna basically subtract from our height
10:08the line width and that's all we really had to do
10:11to create the flag of Israel.
10:14So there we go.
10:15We created another flag
10:16and this one is even more complicated.
10:18We took advantage in this,
10:20both the triangle logic that we've created
10:22a couple of lectures ago
10:23and Ota the cosine and sine logic
10:25that enabled us to create this clock.
10:28Ota enabled us to create this
10:31multi-dimensional object that is based on two objects
10:34wrapped together or two visual triangles wrapped together.
10:38In this lesson, we created the Star of David
10:41and through it we saw how could we leverage
10:43cosine, sine, and radians to create very complex shapes.
10:47We're not done with the complexities
10:49because the step next after creating the Star of David
10:52is to create a star that has multiple vertices.
10:55To do that, we're gonna use actually two circles
10:58and we're gonna learn about that in the next lecture.
