Making a Sine Law Calculator

I was doing grade 10 trigonometry, specifically sine law when I had a thought: what if I coded a program to do this for me?

bigbrain

The idea sounded wrong. I was supposed to do my own homework, not have an AI do it for me. This would be on the same level as using Chatgpt for an essay. However, the questions were incredibly repetitive. They didn’t require any memorization of the exact trig values, such as sin 30° or cos 45°. They simply need you to understand how to use a calculator and plug in the numbers. It felt like busywork to me. However, and this doesn’t apply only for trigonometry, but new math knowledge needs to be constantly practiced for it to actually set into the brain, for the person to understand it and use it well. So how could I prove that I know sine law like the back of my hand? As a coder, the answer came naturally: To make a program that can calculate sine law, no matter the input. However, I wanted to take it a step further. Ironically, finding out the answer wouldn’t be the solution to this. I wanted to write out the process, which should look somewhat like below:

s / sin S = t / sin T
18 / sin (72°) = 15 / sin T
18 / 0.95 * sin T = 15
sin T = 15 / 18 * 0.95
sin T = 0.791
∠T = 52°

This would hopefully be the output that the user receives, and the goal is to minimize the inputs that the user has to give for convenience. After all, the goal of continuing to make advancements in AI is to make our future easier, no?

Such an amazing idea of a machine doing your homework is sadly not my idea. The book “The Homework Machine” has made a deep impression on me. The machine sounded so simple, yet deep down I knew it was sci-fi. It wasn’t as easy as it described, and I have actually always wanted to make something similar to this. So without any more delay, I’ll explain my entire thought process as I coded it.

I decided to use Scratch for the convenience of having pre-made images as well as an easy way to upload it onto the web so I can access it any time. The purpose of this project was to do calculations, not for users to appreciate the amazing graphics. With that in mind, I chose a simple dino from scratch’s library of sprites and made it change costumes everytime it asks a question. This dinosaur is going to continue to pester people with questions until it has enough information. Now, the first thing that I do when starting a calculation project is to consider how many variables I am going to be using. The law of sines can be used to compute the remaining sides of a triangle when two angles and a side are known or to calculate the corresponding side of one of the known angles when two sides and a side corresponding to the other angle is known. The formula is shown as below:

sinelaw

Using this, it can be known that we need at least four variables: side a, side b, angle A and angle B. Now, there is something that needs to be explained before continuing. The sine law is used when two sides and an angle are known to calculate the other angle, or when two angles and a side is known to calculate the other side. This would be two scenarios, in which I decided to take on. I then first made sure which side and angle has to be known in both situations for the calculator to actually work. In other words, we have to know either two angles and a side or two sides and an angle. And so, this gives us these variables so far:

  • Known side 1
  • Known side 2
  • Known angle 1
  • Known angle 2
  • Unknown side 2
  • Unknown angle 2

For anyone confused as for why there is a known side 2 as well as an unknow side 2, remember that we are coding the AI to deal with two scenarios, so the second side can be known or unknown. However, if we go back to the process that we want to output, the problem emerges: we also need names. We can’t just do calculations, otherwise where are the angle names and sides going to come from? So, with this in mind, the variable list doubles:

  • Known side 1
  • Known side 1 name
  • Known side 2
  • Known side 2 name
  • Known angle 1
  • Known angle 1 name
  • Known angle 2
  • Known angle 2 name
  • Unknown side 2
  • Unknown side 2 name
  • Unknown angle 2
  • Unknown angle 2 name

This is where I realized the incredible amount of variables that I was dealing with. The dinosaur was going to ask an incredible amount of questions, and I had to think of a way to reduce that. I took a closer look at the formula and that’s when I found out. You see, angle A in the formula corresponds to side lowercase a, and so, I decided to make a function to convert the inputed values to lowercase and to uppercase. The process had me creating the variable “i”, the variable “lowercase alphabet” and “uppercase alphabet”. After this was done, the AI can now ask less questions. Before, the dinosaur had to ask these questions:

  • Known side 1 value?
  • Known side 1 name?
  • Known side 2 value?
  • Known side 2 name?
  • Known angle 1 value?
  • Known angle 1 name?
  • Known angle 2 value?
  • Known angle 2 name?
  • Unknown side 2 name?
  • Unknown angle 2 name?

Now, for the addition of three variable, we only have to ask 8 questions instead of the original 10.

  • Known side 1 value?
  • Known side 1 name?
  • Known side 2 value?
  • Known side 2 name?
  • Known angle 1 value?
  • Known angle 1 name?
  • Known angle 2 value?
  • Known angle 2 name?

Of course, we still have to add one more question, which is “How many known sides?” This question is an important one, because it is essentially asking the user “Are you trying to solve for an angle or a side”, and if the answer is one, then it should fit under scenario 1 and if the answer is two it would fit under scenario 2.

To make this happen I then used an

if < > then 
else 

to allow the AI to take on the two scenarios. In the boolean, I entered the following:

Known sides = 1

This is terrible grammar, but the variable “known sides” is the one collected from the answer, so it has to be that. This is where I felt like I created a useless variable. Instead of creating this variable, I completely could’ve just used the answer in the boolean like this:

if < Answer = 1> then:
    Scenario 1
Else:
    Scenario 2

Yet instead the actual code looks like this:

if < Known sides = 1> then:
    Scenario 1
Else:
    Scenario 2

The benefit of this is that when people edit it, they would understand it easier. The downside is that I was inefficient with my code, so this is a learning moment for me here.

And at last, after all the required variables have been determined, it is time to do the calculation. I first created a list named “Answers”, which is perfect because list #1 of the answer would be step 1, list #2 would be step 2 and so on. The calculation code itself looks like this, and it certainly isn’t pretty:

bigcode

For the sake of time and for those that are just not interested in these intimidating-looking blocks of code, I won’t go in-depth to explain it. A grossly simplified explanation of the code is that I abused the

join ()()

block for the final result. Now just what is the final result and how do we get to that? As I have previously said, I tried my very best to make this beginner-friendly, but some prerequisite knowledge of triangles are needed.

The link to the calculator is below: https://scratch.mit.edu/projects/565410737/, and upon clicking the green flag, the dinosaur will ask you the question “How many known sides?” I will be using problem A from question 1 from http://www.alamandamaths.com/wp-content/uploads/2018/05/SIN-Law.pdf to demonstrate. If you want to calculate a side, input “1”, otherwise input “2”. For this question I will type 2, because two sides are known, side a and side b. The next question is very self-explanatory: “First known angle value?” asks for the first angle. If you are doing the same as me, just input the only angle that you know. I’ll input 42, and now the AI wants to know the first side value. Now, it is very important that you input the CORRESPONDING side. For me, since the angle I gave was angle B, I MUST input side b for this question. After that, give the AI the second side that you know, which for me is 22. Now it will start asking for names, and the first known angle name in my case is B. When giving names, it is very important to keep in mind that although Scratch booleans agree that B = b, which in other words mean that despite the fact that Scratch itself isn’t case sensitive, this calculator is still case sensitive. I inputted B, and now the first known side name ALSO NEEDS TO BE THE CORRESPONDING SIDE OF THE ANGLE YOU HAVE GIVEN. In my case, it is b. The second known side name is lowercase a, and after that, voila! The answer is there for you to copy paste.

problem

The answer that the calculator gave is the following:

answer
Which matches with the answer of the website
answer2

I will also briefly cover what happens if you inputted “1” to the first question. The dino will ask you the first known angle value, which you need to give the angle that corresponds to the known side. The next two questions are simple quesitons. When it starts asking for names, just keep in mind that this calculator is case sensitive, and you should be good to go!


Comments: