#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Apr  7 12:45:04 2025

@author: andreagarulli
"""
import numpy as np
import scipy
import matplotlib.pyplot as plt

def plotellipse(A, b, c, color=None):
    # function that plots the ellipse x'Ax+2b'X+c<0
    n = 2
    D, V = np.linalg.eig(A)
    alfa = b.T @ V

    cen = -alfa / D
    dime = alfa**2 / D
    dimes = np.sum(dime) - c
    l = np.sqrt(dimes / D)

    ax = V @ np.diag(l)

    N = 360
    X = np.zeros((n, N))
    for j in range(N):
        th = j * 2 * np.pi / N
        X[:, j] = cen + np.array([l[0] * np.cos(th), l[1] * np.sin(th)])

    X = V @ X  # trasformazione alle coordinate originali
    cen = cen @ V.T  # centro nelle coordinate originali

    if color is not None:
        plt.plot(X[0, :], X[1, :], color)
        
    return X, cen, ax
    

