Code source de ensae_teaching_cs.special.image.image_synthese_facette

# -*- coding: utf-8 -*-
"""
définition d'une facette


:githublink:`%|py|6`
"""
import math
from .image_synthese_base import Objet


[docs]class Facette (Objet): """ définit un triangle dans l'espace :githublink:`%|py|11` """
[docs] def __init__(self, a, b, c, couleur): """ initialisation :githublink:`%|py|14` """ self.a, self.b, self.c = a, b, c ab = b - a ac = c - a self.vnorm = ab.vectoriel(ac) self.vnorm = self.vnorm.renorme() self.couleur = couleur
[docs] def intersection_plan(self, r): """ retourne le point d'intersection entre le plan et le rayon r :githublink:`%|py|23` """ if r.direction.scalaire(self.vnorm) == 0: return None oa = self.a - r.origine lv = self.vnorm.scalaire(oa) / self.vnorm.scalaire(r.direction) p = r.origine + r.direction * lv return p
[docs] def point_interieur(self, p): """ dit si un point appartient à l'intérieur du triangle :githublink:`%|py|32` """ pa = self.a - p pb = self.b - p pc = self.c - p theta = pa.angle(pb, self.vnorm) theta += pb.angle(pc, self.vnorm) theta += pc.angle(pa, self.vnorm) theta = abs(theta) return theta >= math.pi * 0.9
[docs] def intersection(self, r): """ retourne le point d'intersection avec le rayon r, retourne None s'il n'y pas d'intersection :githublink:`%|py|44` """ p = self.intersection_plan(r) if p is None: return None if self.point_interieur(p): return p else: return None
[docs] def normale(self, p, rayon): """ retourne la normale au point de coordonnée p et connaissant le rayon :githublink:`%|py|54` """ if rayon.direction.scalaire(self.vnorm) < 0: return self.vnorm else: return - self.vnorm
[docs] def couleur_point(self, p): """ retourne la couleur au point de coordonnée p :githublink:`%|py|61` """ return self.couleur
[docs] def __str__(self): """ affichage :githublink:`%|py|65` """ s = "facette --- a : " + str(self.a) s += " b : " + str(self.b) s += " c : " + str(self.c) s += " couleur : " + str(self.couleur) return s
[docs]class Rectangle (Facette): """ définit un rectangle dans l'espace :githublink:`%|py|74` """
[docs] def __init__(self, a, b, c, d, couleur): """ initialisation, si d == None, d est calculé comme étant le symétrique de b par rapport au milieu du segment [ac] :githublink:`%|py|78` """ Facette.__init__(self, a, b, c, couleur) if d is not None: self.d = d else: i = (a + c) / 2 self.d = b + (i - b) * 2
[docs] def point_interieur(self, p): """ dit si un point appartient à l'intérieur du triangle :githublink:`%|py|87` """ pa = self.a - p pb = self.b - p pc = self.c - p pd = self.d - p theta = pa.angle(pb, self.vnorm) theta += pb.angle(pc, self.vnorm) theta += pc.angle(pd, self.vnorm) theta += pd.angle(pa, self.vnorm) theta = abs(theta) return theta >= math.pi * 0.9
[docs] def __str__(self): """ affichage :githublink:`%|py|100` """ s = "rectangle --- a : " + str(self.a) s += " b : " + str(self.b) s += " c : " + str(self.c) s += " d : " + str(self.d) s += " couleur : " + str(self.couleur) return s