Difference in Angles for Plugin

Currently, I’m working on a plugin and want to calculate the smallest angle between the lines that two parts make.

Old Image

The black line represents the line of the part, and the green is the angle I want (smallest angle made between the parts).

Here’s a video of how my work, and what doesn’t work:

The black text above the red text is the angle calculated.

As you can see, the angles calculated when my mouse is on the left side are not correct.

In the video:

Left Side (wrong):

Right Side (correct, I want this on the left side, too):

The code for it is calculated here with angle1 being part1 (the part that moves with the mouse)'s Y orientation, and angle2 being part2 (the part that is horizontal and not moving)'s orientation.

AngleDifference = function(angle1,angle2,modulusInt)
		local modulusInt = modulusInt or 360
		local indicator = angle1%modulusInt - angle2%modulusInt
		return indicator%90,indicator
		--return angle1%modulusInt - angle2%modulusInt
	end,

The black text in the video is indicator%90, while the red text is indicator.


Do any of y’all know how to calculate angles between lines/parts correctly?

I appreciate any help on how to fix this or how to calculate the angle equation.

By the way

I don’t need help calculating when the angle is not 0 or a multiple of 90, as my plugin doesn’t need to make this calculation at right/straight angles.

Here are the four possible ways an angle is calculated between line parts:

Solved this myself.

My table of functions used:

cFrameFunc = {
	PositiveLineAngle = function(angle)
		return ((angle < 0) and (angle + 180) or angle)%180 -- %180 so that 180 becomes 0
	end,
	
	AngleDifference = function(angle1,angle2,modulusInt)
		return math.abs(angle1 - angle2)%modulusInt,angle1 - angle2
	end,
	
	-- (other functions)
}

The code involved:

local pastAngle1,pastAngle2 = lucentPart.Orientation.Y,n.Orientation.Y
local angle1,angle2 = cFrameFunc.PositiveLineAngle(pastAngle1),cFrameFunc.PositiveLineAngle(pastAngle2)
local pastZ,indicator = cFrameFunc.AngleDifference(angle1,angle2,90)

In action: