Reputation: 2860
I want to calculate the slope in % for example i just have the angle: (maybe in PHP or Javascript) slope(22.5) //50%
0=0%
22.5=50%
45=100%
67,5=50%
90=0%
112,5=50%
135=100%
180=0
202,5=50%
225=100%
270=0
Upvotes: 2
Views: 1854
Reputation: 51
Using similar reasoning as paxdiablo I came up with this one-liner
slope = 100*abs(((angle+45) mod 90)-45)/45;
You should be able to see the plot here: go to wolframalpha
Upvotes: 1
Reputation: 881293
It appears based on your data that, in each quadrant, you linearly rise from 0 to 100% in the first 45 degrees and drop back down to 0% in the second 45 degrees.
So, you can map all angles into the first quadrant, 0 <= angle < 90
, with:
angle = angle % 90
Then if it's in the second half of that quadrant, transform it with a rotation around the 45 degree line with:
if angle >= 45:
angle = 90 - angle
Now you have an angle 0 <= angle < 45
that's effectively the "distance" from the nearest quadrant boundary, and you can do:
percent = angle * 100 / 45
to get that expressed as a percentage.
So, in short:
angle = angle % 90
if angle >= 45:
angle = 90 - angle
percent = angle * 100 / 45
As proof of concept, here's some Python code that shows it in action:
for i in range (361):
angle = i % 90
if angle >= 45:
angle = 90 - angle
percent = angle * 100 // 45
print("%d -> %d" % (i, percent))
along with the abridged output:
0 -> 0
1 -> 2
2 -> 4
3 -> 6
::
43 -> 95
44 -> 97
45 -> 100
46 -> 97
47 -> 95
48 -> 93
49 -> 91
::
87 -> 6
88 -> 4
89 -> 2
90 -> 0
91 -> 2
92 -> 4
93 -> 6
::
352 -> 17
353 -> 15
354 -> 13
355 -> 11
356 -> 8
357 -> 6
358 -> 4
359 -> 2
360 -> 0
Upvotes: 2