Working with sine, cosine and radians
As you start to develop more complex shapes you will need to take advantage of the sin and cosine to create complex shapes and animations. In this lecture we will remind you of the basics of working with the sin and cosine as we get the ground ready to build a analog clock flag.
Keynotes:
- Working with sin and cosine.
- Finding points in a circle.
- Understanding radians.
Video Transcript
00:01The next section is gonna be dedicated completely to angles
00:05but even more of that, the three main topics really
00:09are gonna be radians, cosine, and sine.
00:13Through this section, we're gonna go ahead
00:15and get introduced to those concepts.
00:18We're gonna create together an animating clock
00:21with the leverage of that knowledge
00:22that we built off of sine, cosine, and radians.
00:26We're gonna create a lot more complex shapes
00:29using the capabilities that stand behind
00:31sine, cosine, and radians.
00:34And we're gonna add a lot more vertices.
00:37You're gonna understand it very deeply
00:39because by the end of this session,
00:41you'll know to create even stars with multiple points.
00:45Really cool stuff.
00:47We're also gonna learn how to overlap shapes
00:49with other shapes to create more complex shapes.
00:53So that's it.
00:54Let's jump into our first lesson.
00:56In the first lesson in this section,
00:57we're gonna work with getting to know
00:59the sine, cosine, and radians.
01:02We're gonna work with sine and cosine and understand them.
01:05We're gonna find specific points in the circle
01:07using radians, and we're gonna understand
01:09what radians are at all and why are we working
01:11with radians and not with just traditional angles
01:15that you're familiar with that are from zero to 360.
01:18So let's jump right into that first lecture.
01:21Let's see what are sine and cosine and radians.
01:25We're gonna take a small break from creating flags.
01:29And what I wanna do is I'm gonna go right into our
01:31due canvas, we're gonna create together a clock.
01:35And the goal of this creation is to show you
01:37how to work with the sine and cosine.
01:40So let's go ahead and I'm just gonna duplicate
01:42that creation of the kingdom of Bahrain
01:45and then we'll change it.
01:46And I'm just gonna make sure that I'm sending
01:48into the original Bahrain sample a new canvas element.
01:53Beautiful, and for our create,
01:57the new one we'll call it create time.
02:01And let's duplicate that method name,
02:03go all the way to the bottom where we had our create Bahrain
02:06and I'm just gonna create a new function here.
02:09And let me just copy all the basic stuff that we have
02:13from the beginning, from each sample that we have there.
02:16And I'm just gonna make sure that we have here
02:19our basic starting point.
02:22Beautiful, so let's start by now that we have
02:24our basic kind of configuration set up there.
02:27Let's go ahead and just create here the real basics.
02:31What do we want in our very, very basics
02:33is literally we're gonna clear our screen
02:36by just creating a nice little shape here.
02:38I'm gonna go to my context.
02:40I'm gonna fill style, set a fill style
02:43and just define it to a color.
02:44And I already just picked a random color here
02:47that I pre-configured originally.
02:50And I'm gonna go ahead and fill a rectangle.
02:53Now even before we check this out,
02:54I'm just gonna go ahead and just set it
02:56to be the full width and height.
02:57We've done this many times.
02:59I wanna revisit the arc which we haven't seen for a while.
03:02We've seen it only in our first lecture
03:04and kind of get a better understanding
03:06of how the arc works.
03:09So I'm gonna go into my context
03:10and I'm gonna create a new arc.
03:12Now even before that, I wanna go ahead
03:14and change the fills color.
03:16So I'm gonna change that fill style
03:19to be a different color.
03:20So our circle won't be the exact same color
03:22as our rectangle that has drawn our background.
03:26The next stages for our arc,
03:28we wanna define for it a few coordinates.
03:31First of all, it's center point.
03:32So to do that, I'm gonna go ahead
03:34and add another variable that we're gonna be using
03:36throughout our application.
03:37One will be x which will give us the center point
03:40of our application.
03:42And our next will be our height or y
03:45which will be our height divided by two
03:46giving us the exact center point
03:48of our mimic flag that we were creating.
03:52Beautiful.
03:53So we're gonna send in that x and y
03:54as our first coordinates.
03:56The next coordinate that we wanna send in
03:58is a radius and I'm gonna go ahead
04:00and store that also because we're gonna use it
04:02throughout this lecture.
04:03And I'm gonna go ahead and make sure
04:04that the radius will be a third
04:06of the height of our screen.
04:08Making sure that it's taking most of our screen
04:10but not all of it.
04:12All right, so we have our third property of our arc.
04:16The next two properties are the properties
04:18that define from what point in the angle
04:21will it start and to what point.
04:23And as we said previously as a reminder,
04:25so we said we'll start from zero.
04:26But we said that a circle is represented by two pis.
04:30And in programming many times we don't work
04:32with degrees directly like 360
04:35but instead of that we work with pis.
04:37So I'm gonna set my math pi
04:39and that's gonna give us our radian.
04:40So it's a radian, very similar to radius
04:43but a radian is basically giving us
04:46that we have 360 here although in our normal flow
04:50but it's two pi r, the radius of our circumfier
04:55of our circle.
04:57Okay, last but not least I'm gonna set it to false
04:59although it doesn't really matter
05:00if our direction is counter clockwise or clockwise.
05:04It only matters when our value here
05:06is not a complete circle.
05:07In our case again we're creating a complete circle.
05:10And I'm gonna go ahead and set my context
05:13to fill that item out.
05:15Now if we go back into our browser
05:17we should see that we should have here now
05:19at this stage a nice little flag
05:21that's kind of like correlates
05:22to our basic colors in our application.
05:26The next step that I really wanna do
05:27is define actually the clock widget's time.
05:30But before we even do that I wanna take a focus on
05:33let's see how do we get to the different points
05:35of the circle.
05:36The points that are the most easy and most obvious
05:38let's go ahead and give that a try.
05:40So for the most obvious and most I would say
05:44direct points I'm gonna go to my context
05:47and I'm gonna set my stroke style
05:49and I'm gonna go ahead and set it to be a green
05:51and I'm gonna go ahead and just copy a green
05:53that I already pre-configured earlier.
05:56And I'm gonna set it a width.
05:57So let me give it a line width of let's say five
06:02and I'm gonna go ahead and begin a new path.
06:05And once I began that new path
06:07I'm gonna go ahead step by step
06:09starting from removing my context
06:12moving my I would say my pointer to what point?
06:16I wanna move it to well the center point
06:18is the xy coordinate but I don't wanna be in the center.
06:21What I really want it to be is I want it to be
06:23in my left line let's say.
06:26So I'm gonna subtract the r the radius from our x position
06:30and then I'm gonna draw a line to a plus to that r.
06:37Now that's gonna give me the two points
06:39of the two edges of our circle.
06:41So if I go ahead here and save this
06:42and go back and click on refresh
06:44you'll see that now we're gonna have oh
06:45and I need to also make sure
06:46that I'm calling the stroke command.
06:53There we go and let's go ahead and give it another shot.
06:56And here we go we can see that we now drew a line
06:58that went exactly in the radius
07:00and the dots of my radius of my circle.
07:04Using that radius cause I knew that center point
07:06it was very easy for me to find those corners.
07:08Let's go ahead and also find the top corners
07:10which literally will be the exact same logic
07:12only this time we're gonna apply it to our y.
07:16Those were the easy parts to find.
07:18Contrary to that if I wanted to find a point
07:21that is in a specific angle I wouldn't just be able
07:23to do just regular plus minus signs
07:26to be able to figure out and configure
07:28a specific point in time.
07:31So now things get a little bit more complicated
07:34and the complication is that we really need to find dots
07:37that are in other spaces not only these perfect edges
07:40which are very easy for us to configure using a radius.
07:44To be able to do that we're gonna use
07:46basically some mathematics.
07:48And the idea behind here is finding the sine and cosine.
07:51Now because a circle is a perfect shape
07:53it really doesn't matter if we set the sine
07:55for the x or the cosine for the y
07:58as long as we're putting in the same values on both ends.
08:01And when we put the same values on both ends
08:04which we're gonna see it in action momentarily.
08:06What's actually happening is that the math class
08:10is calculating for us a representation of a circle
08:13with a radius of one.
08:14It's x, y positions relative to its center.
08:19So it's basically assuming that there's a zero, zero coordinate
08:22and that our position for example
08:24if we're trying to find the spot here
08:26what we really are trying to get in this triangle example
08:29we're trying to get the x and the y coordinates.
08:31Or really what we're trying to get
08:33is the length of this item and the length of this item.
08:38Because if we get the length of this item
08:39what are we getting?
08:40We're getting here the value of our width
08:44or our yeah, our width.
08:46And here we're gonna get the value of our height.
08:49Now because we already have the radius
08:51we have the angle because it's a perfect triangle.
08:55So what we could do is we could use the sine
08:57and cosine to get those information.
08:59So the sine itself is gonna give us the adjacent
09:01which is basically the theoretical width.
09:04And the cosine is gonna give us the value of our height
09:08which in both cases is gonna give us the information
09:11that we need combined to be able to figure out
09:13the exact point.
09:14Now because we're working with a circle
09:17it doesn't really matter too much
09:18if you put first sine or cosine.
09:21All that really matters is that you make sure
09:23that you offset the value
09:24because we really wanna offset that zero
09:26will be on the top right here.
09:28So let's see what I mean.
09:29Let's go right into our application
09:30and I'm gonna go ahead and just get rid of
09:33all this extra lines that we've put here.
09:35And what I wanna do is I'm gonna just create one line.
09:38But instead of creating this one line
09:39and actually defining it in,
09:42I'm just gonna go to the center point
09:43and what I wanna do is I wanna draw a line
09:46and I'm gonna use for that matter
09:47I'm gonna still use the x and y
09:49but I'm gonna add to them that cosine and sine values.
09:53So let's start with the sine.
09:55So I'm gonna go to the math that sine
09:57and I'm gonna go ahead and send to it a radius of zero
10:00and then duplicate it by our r
10:02which literally what we're saying here
10:04is I'm sending here a degree of zero degrees
10:06but the ranges of degrees as we said previously
10:10when we were working with our arc
10:12is that the range is not from zero to 360
10:14but from zero to two pi.
10:17All right, so I'm gonna go ahead and also do
10:19the exact same thing for our y coordinate
10:21and with our y coordinate I'm just gonna go ahead here
10:24instead of using a sine I'm gonna use a cosine.
10:26Now it really doesn't matter in this case as I said before
10:29if you use sine or you use cosine for each.
10:32The only thing that matters is when you take
10:34the zero zero coordinate, the zero zero basic point
10:38and you find out that your zero zero in this case is here
10:41then you're gonna need to subtract 90 degrees
10:43to be able to get to your initial position
10:46or in this case what we would have to do
10:47is subtract math.pi and by subtracting math.pi
10:52then we would get to that initial position
10:54that we wanted to get to.
10:55So if I go ahead here and just subtract math.pi
10:58and go ahead and click on refresh
10:59then we'll have our starting position
11:01at our humanly visible zero
11:03which will then be used for our clock.
11:05Now on the contrary if you use your cosine first
11:08and then you use the sine then you're gonna need
11:11to use a different algorithm, a different logic
11:13to figure out your starting point
11:15and that's the only thing that would matter
11:17in a case of a perfect circle
11:19that you start off and put that offset.
11:21So in this case if you started off with a cosine
11:23and a sine then you would make sure
11:25that your offset is not gonna be a full math.pi
11:27but a half of that math.pi
11:29and I'm gonna go ahead and just get that offset into it
11:32making sure that I'm gonna start off
11:34from the right position when I'm working with my clock.
11:37Now in the next lecture we'll actually start
11:39building our clock now that we get the basic logic
11:42of cosine and sine and continue working
11:44with cosine and sine to build an actual clock
11:47that's gonna be ticking away.
11:49So we'll see you in the next lecture when we do that.
11:52In this lesson we really got introduced
11:54to the main topic of this section
11:56which is radians, sine and cosine.
11:59In the next lecture we're gonna take advantage
12:01of all those capabilities to create our first
12:04animating element inside of Canvas.
12:07It's gonna be a clock that's ticking.
