# JavaScript Math Issues

So I’m building a ‘radar’ for a game. It consists of a background png and some JavaScript code that allows the user to click and drag to draw a line. Now the code is meant to display the ‘heading’ of the line.

If anyone fancies aviation, you know that planes fly in headings like a compass. This radar is meant to do that, but for some reason the code that I used, when I drag the mouse perpendicularly upwards, the heading displays 180 when it should be 360/000 degrees (north), when I drag to the right the heading is 090, which is correct, when I drag downwards the heading is 360/000 when it should be 180 degrees (south), and when I drag the heading to the left the heading is correct.

So it seems like the “compass” is flipped vertically. The range should be 360 degrees moving clockwise. Can anyone help? I’m pretty new to coding.

``````const radar = document.getElementById('radar');
let isDragging = false;

if (line) {
line = null;
}
}
}

e.preventDefault();
});

startX = e.clientX - rect.left;
startY = e.clientY - rect.top;
isDragging = true;

line = document.createElement('div');
line.className = 'line';
line.style.left = `\${startX}px`;
line.style.top = `\${startY}px`;

});

if (!isDragging) return;

const currentX = e.clientX - rect.left;
const currentY = e.clientY - rect.top;

const deltaX = currentX - startX;
const deltaY = currentY - startY;

const length = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
let angle = Math.atan2(deltaY, deltaX) * (180 / Math.PI);

if (angle < 0) {
angle += 360;
}

angle = (90 - angle + 360) % 360;

line.style.width = `\${length}px`;
line.style.transform = `rotate(\${angle}deg)`;

displayHeadingText(angle, (startX + currentX) / 2, (startY + currentY) / 2);
});

if (isDragging) {
isDragging = false;
}
});

if (isDragging) {
isDragging = false;
}
});

}

}

}``````
``````body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}

position: relative;
}

#map-container {
position: relative;
overflow: hidden;
}

#map {
display: block;
border: 2px solid black;
background-color: black;
position: relative;
z-index: 1;
}

.map-background {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: black;
z-index: 0;
}

.line {
position: absolute;
height: 4px;
background-color: yellow;
transform-origin: top left;
z-index: 2;
}

position: absolute;
background-color: white;
border: 1px solid black;
transform: translate(-50%, -50%);
z-index: 3;
}``````
``````<!DOCTYPE html>
<html lang="en">

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">