How to build a roof with intersecting walls?

I’ve made a wall-building script like this:

And now I am trying to build different roofs with different areas, but I can only figure out the solution when the area is a square.

A square has four vertices. I calculate the smallest one and the largest one, then we can get the cframe of a square part.

I describe an area with a line class:

local LineClass = {
	id = nil, 
	startPos =,0), 
	endPos =,0), 
	__eq = function(line1, line2)
		if line1.startPos == line2.startPos and line1.endPos == line2.endPos then
			return true
			return false
local AreaClass = {
	linesTable = {}, -- LineClass
	pointsTable = {}, -- Vector2
	hasRoof = false

​I was trying to build two roofs, but could not make out when the area was not square.

Anyone got any ideas?