Функция в Javasript, которая принимает радиус «r» (в сантиметрах) часов и угол «градус» часовой стрелки
Функция в Javasript, которая принимает радиус «r» (в сантиметрах) часов и угол «градус(alpha)» часовой стрелки. На основе этих параметров ваша функция вычислит положение минутной стрелки и вернет область «S» меньшего сектора между двумя стрелками.
function a(r, alpha) {
var m = 0,
h = 0,
s = 0;
h = alpha / 30;
m = 60 * (h - Math.floor(h));
m = Math.abs(30 * Math.floor(h) - m * 6);
angle = Math.min(m, 360 - m);
s = (Math.PI * Math.pow(r, 2) * angle) / 360;
return s;
}
document.write(a(10, 130))
что не так?
Ответы (2 шт):
Автор решения: Igor
→ Ссылка
Предполагается, что обе стрелки движутся плавно, без рывков.
function a(r, alphaH) {
var alphaM = (alphaH - Math.floor(alphaH / 30) * 30) / 30 * 360;
var delta = alphaH > alphaM? (alphaH - alphaM) : (alphaM - alphaH);
if (delta > 180)
delta = 360 - delta;
console.log("Angles: h =", alphaH, ", m =", alphaM, ", delta =", delta);
return Math.PI * r * r * delta / 360;
}
console.log("Sector area:", a(10, 135).toFixed(3));
console.log("Sector area:", a(10, 130).toFixed(3));
console.log("Sector area:", a(10, 180).toFixed(3));
console.log("Sector area:", a(10, 270).toFixed(3));
Автор решения: eustatos
→ Ссылка
const submitButton = document.getElementById('submitButton');
submitButton.addEventListener('click', handleSubmit);
function handleSubmit(e) {
e.preventDefault();
const radiusInput = document.getElementById('radius');
const radius = radiusInput.value;
const angleHourInput = document.getElementById('angle');
const angleHour = angleHourInput.value;
const hour = Math.floor(angleHour / 30);
const minute = (angleHour - hour * 30) * 2;
const angleMinute = minute * 6;
const delta = Math.abs(angleMinute - angleHour);
const S = Math.PI * Math.pow(radius, 2) * delta / 360;
console.log('radius', radius);
console.log('angleMinute', angleMinute);
console.log('delta', delta);
console.log('S', S);
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<form class="form container">
<div class="form-group">
<label for="radius">Radius</label>
<input id="radius" class="form-control">
</div>
<div class="form-group">
<label for="angle">Angle</label>
<input id="angle" class="form-control">
</div>
<button id="submitButton" class="btn btn-primary">Calculate</button>
</form>