Last active
January 28, 2020 18:20
-
-
Save arget13/ba35eb38850038cefce47d9e45eb57a4 to your computer and use it in GitHub Desktop.
Have you ever wanted to be a shitty pedant? Now you can tell time in degrees or radians!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <math.h> | |
// 2pi / (1440 / 2) | |
#define ratiorad 0.0087266 // radians that one minute equals | |
// 360 / (1440 / 2) | |
#define ratiodeg 0.5 // degrees that one minute equals | |
#define PI2 6.2831853 | |
#define rad2deg 57.2957795 | |
int main(int argc, char** argv) | |
{ | |
int h, m; | |
float a; | |
if(argc < 2) return -1; | |
if(strchr(argv[1], ':')) | |
{ | |
if(sscanf(argv[1], "%d:%d", &h, &m) != 2) return -1; | |
m += (h % 12) * 60; | |
if(m >= 0 && m <= 180) // 1st quadrant [12h, 3h] | |
{ | |
printf("%g°\t", (180 - m) * ratiodeg); | |
printf("%g rad\n", (180 - m) * ratiorad); | |
} | |
else | |
if(m > 180 && m < 540) // 4th and 3rd quadrant (3h, 6h) U (6h, 9h) | |
{ | |
printf("%g° (%g°)\t", 360 - (m - 180) * ratiodeg, -(m - 180) * ratiodeg); | |
printf("%g rad (%.5g rad)\n", PI2 - (m - 180) * ratiorad, -(m - 180) * ratiorad); | |
} | |
else | |
if (m >= 540) // 2nd quadrant [9h, 12h) | |
{ | |
printf("%g°\t", 360 + (180 - m) * ratiodeg); | |
printf("%g rad\n", PI2 + (180 - m) * ratiorad); | |
} | |
} | |
else | |
{ | |
if(sscanf(argv[1], "%g", &a) != 1) return -1; | |
// If we've radians convert them to degrees | |
if(argc == 3) | |
a *= rad2deg; // It's easier to operate with degrees | |
// mod 360 | |
a -= (int)(a / 360) * 360; | |
if(a < 0) a += 360; | |
if(a >= 0 && a <= 90) // 1st quadrant | |
m = 180 - (a / ratiodeg); | |
else | |
if(a > 180 && a < 360) // 3rd and 4th quadrant | |
m = (360 - a) / ratiodeg + 180; | |
else | |
if(a <= 180) // 2nd quadrant | |
m = 180 - (a - 360) / ratiodeg; | |
printf("%02d:%02d\n", !(m / 60) ? 12 : m / 60, m % 60); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<input type="text" id="hour"> | |
<button onclick="hour2angle();">To angle!</button> | |
<button onclick="angle2hour(0);">From radians to time!</button> | |
<button onclick="angle2hour(1);">From degrees to time!</button> | |
<div id="result"></div> | |
<script type="text/javascript"> | |
// 2pi / (1440 / 2) | |
var ratiorad = 0.0087266; | |
// 360 / (1440 / 2) | |
var ratiodeg = 0.5; | |
var pi2 = 6.2831853; | |
var rad2deg = 57.2957795; | |
var h, m; | |
var a; | |
function hour2angle() | |
{ | |
hour = document.getElementById('hour').value; | |
result = document.getElementById('result'); | |
h = parseInt(hour); | |
m = parseInt(hour.split(":")[1]); | |
m += (h % 12) * 60; | |
if(m >= 0 && m <= 180) // 1st quadrant [12h, 3h] | |
{ | |
result.innerHTML = (180 - m) * ratiodeg; | |
result.innerHTML += "° "; | |
result.innerHTML += (180 - m) * ratiorad; | |
result.innerHTML += "rad"; | |
} | |
else | |
if(m > 180 && m < 540) // 4th and 3rd quadrant (3h, 6h) U (6h, 9h) | |
{ | |
result.innerHTML = 360 - (m - 180) * ratiodeg + "° (" + -(m - 180) * ratiodeg; | |
result.innerHTML += "°) "; | |
result.innerHTML += pi2 - (m - 180) * ratiorad + "rad (" + -(m - 180) * ratiorad; | |
result.innerHTML += "rad)"; | |
} | |
else | |
if (m >= 540) // 2nd quadrant [9h, 12h) | |
{ | |
result.innerHTML = 360 + (180 - m) * ratiodeg; | |
result.innerHTML += "° "; | |
result.innerHTML += pi2 + (180 - m) * ratiorad; | |
result.innerHTML += "rad"; | |
} | |
} | |
function angle2hour(degrees) | |
{ | |
a = parseFloat(document.getElementById('hour').value); | |
result = document.getElementById('result'); | |
// If we've radians convert them to degrees | |
if(!degrees) | |
a *= rad2deg; // It's easier to operate with degrees | |
// mod 360 | |
a -= Math.floor(a / 360) * 360; | |
if(a < 0) a += 360; | |
if(a >= 0 && a <= 90) // 1st quadrant | |
m = 180 - (a / ratiodeg); | |
else | |
if(a > 180 && a < 360) // 3rd and 4th quadrant | |
m = (360 - a) / ratiodeg + 180; | |
else | |
if(a <= 180) // 2nd quadrant | |
m = 180 - (a - 360) / ratiodeg; | |
result.innerHTML = (!(m / 60) ? 12 : Math.round(m / 60)) + ":" + Math.round(m % 60); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment