It looks like you're new here. If you want to get involved, click one of these buttons!

I have two questions. Code below is reconstructed from memory, i don't have my ipad with me, so it might be wrongly coded... oh well.

the first example is supposed to be a sine calculator (basically math.sin). I can't get it to increase accuracy beyond a certain number of digits. I don't need that accuracy now (and probably never), but I wanted to be able to do it

local function factorial(n)
assert( n > 1 )
local v = 1
for x= 2, n do
v = v * x
end
return v
end
function mySine(number)
local power = 3 -- n^3/3!, n^5/5!, ...
local result = number
for x = 1, 10 do
if x % 2 == 1 then -- subtract
result = result - number ^ power/factorial(power)
else
result = result + number ^ power/factorial(power)
end
power = power + 2
end
return result
end

Second question

I have a ship class. So it's a really simple Asteroids style ship. I tried using the Law of Sines to calculate the sides, but it didn't work right (so Law of sines is side a/sine(A) = side b/sine(B) = side c/sine(C) ) (i got what I felt was the wrong values when I inputted a known angle to a known side)

Umm, that wasn't the question. The question is:

Where is the y position on this triangle of point c

point a is at 0,0

point b is at 24,0

point c is at 12, ?

angle a is 70*

angle b is 70*

angle c is 40*

```
local y
-- y should be calculated from the angles of the triangle.
--
mesh = mesh()
mesh.vertices = {vec2(0,0), vec2(24,0), vec2(12, y)}
-- angle at 0,0 is 70*
-- angle at 24,0 is also 70*
-- angle at the third point should be 40*
```

## Comments

As for your first question, the number that is being added and subtracted to the

`result`

is getting small really fast. After a few calculations, the value is too small for Codea's 64 bit math so basically you’re just adding and subtracting almost 0. So no matter how many times you try, the`result`

isn’t changing.As for you second question, the law of sines will give you the length of the side opposite the angle. It won’t give you the coordinates of the corners. You need to do other calculations to get the corner coordinates.

You need tan to work out that coordinate:

`y = 12 math.tan(math.rad(70))`

.@xThomas Here’s another version of the sine series. This is about the best resolution you can do before the math used by Codea breaks down. This is 9 iterations in the sine function.

@xThomas Here’s something I found fun writing. You can vary the x,y values and angles of points 1 and 2 and calculate the x,y values and angle of point 3.

@dave1707 I'm still hoping that one day you'll see the benefit of vectors ...

Here's a more efficient version of the sine serie. Rather than work out the factorials afresh for each coefficient, it uses the previous one to work out the next one.

@LoopSpace I’d like to use vectors, but for some reason I don’t seem to fully understand why they work. I’m used to code where I can look at a line of code and know what it does and why. The vector code seems to do things that I don’t see. For instance, in your version of the function calcP3, none of that code tells me anything. I know what the commands do, but why they work eludes me. Maybe the more you replace my code with vectors, the more I’ll start to understand them.

@LoopSpace Here’s another version eliminating the fac function. I only go to 19 in the for loop because 21 causes a wrong calculations in Codea math.

Thank you very much!

@dave1707 I tried to reply to your post on my profile page, but I don't think notifications work there, so I'm reposting it here.

In 2D, you can describe a line by picking a vector orthogonal to it, say

`n`

, and a point on it, say`a`

, and then the line consists of all points`x`

such that`n:dot(x) = n:dot(a)`

. The vector`n`

can be found by taking a vectoralongthe line and rotating it 90 degrees (which is why all the rotates are in the code). This description is very useful for testing a vector to see if it is on the line, but isn't useful for generating vectors on that line.Another description, which interchanges the pros and cons of the previous method, uses a point on the line, say

`b`

, and a vector along it, say`d`

. Then vectors on the line are of the form`b + t * d`

for any number`t`

.We can use these descriptions to find the intersection of two lines. Given

`n`

and`a`

in the first description for the first line and`b`

and`d`

in the second description for the second line, we're looking for`t`

such that`b + t * d`

is on the first line. That is, we want`t`

such that:Since the dot product is linear, we can rewrite this as:

and this rearranges to:

which can be rearranged a little bit more to:

Substituting back in, we get that the intersection of two lines is given by:

which is pretty much what's in

`calcP3`

. Looking again at that code, I see that my dot products are the other way round, but since`u:dot(v) = v:dot(u)`

then that doesn't matter.I hope that helps a bit!

@LoopSpace Thanks for the explanation. I dont know what it says, but I’ll have to get out some graph paper and work out what you’re saying line by line. I seem to understand things better if I can see a picture of what’s happening then by reading about what’s happening.