"""Primitiva GL_LINES — segmentos a partir dos mesmos vértices do círculo. Aula 02 — Visualização 2D: primitivas gráficas. Exemplo em Python com PySide6 e PyOpenGL (pipeline fixed-function). Execute com: python linhas2d.py Este exemplo usa **exatamente os mesmos vértices** de ``pontos2d.py``. Só muda o argumento de ``glBegin`` — e o resultado é completamente diferente. Troque ``PRIMITIVA`` por ``GL_LINE_STRIP`` (polilinha aberta) ou ``GL_LINE_LOOP`` (polilinha fechada) e compare: * ``GL_LINES`` — pares independentes: (v0,v1), (v2,v3), ... -> traços soltos * ``GL_LINE_STRIP`` — v0-v1-v2-... -> polígono aberto (falta fechar) * ``GL_LINE_LOOP`` — como o STRIP, mas liga o último vértice ao primeiro """ from __future__ import annotations import math import sys from PySide6.QtCore import Qt from PySide6.QtGui import QSurfaceFormat from PySide6.QtWidgets import QApplication from PySide6.QtOpenGLWidgets import QOpenGLWidget from OpenGL.GL import * from OpenGL.GLU import gluOrtho2D RAIO = 20.0 PASSO = math.pi / 7.0 # Experimente: GL_LINES | GL_LINE_STRIP | GL_LINE_LOOP PRIMITIVA = GL_LINES class Linhas2D(QOpenGLWidget): """Desenha segmentos de reta a partir de um círculo de vértices.""" def initializeGL(self) -> None: glClearColor(0.10, 0.11, 0.13, 1.0) def resizeGL(self, w: int, h: int) -> None: w, h = max(1, w), max(1, h) glViewport(0, 0, w, h) glMatrixMode(GL_PROJECTION) glLoadIdentity() if w <= h: gluOrtho2D(-50.0, 50.0, -50.0 * h / w, 50.0 * h / w) else: gluOrtho2D(-50.0 * w / h, 50.0 * w / h, -50.0, 50.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() def paintGL(self) -> None: glClear(GL_COLOR_BUFFER_BIT) glColor3f(0.92, 0.92, 0.95) glLineWidth(3.0) # espessura do traço, em pixels glBegin(PRIMITIVA) ang = 0.0 while ang < 2 * math.pi: glVertex2f(RAIO * math.cos(ang), RAIO * math.sin(ang)) ang += PASSO glEnd() def keyPressEvent(self, event) -> None: if event.key() == Qt.Key.Key_Escape: self.close() def main() -> int: fmt = QSurfaceFormat() fmt.setVersion(2, 1) fmt.setProfile(QSurfaceFormat.OpenGLContextProfile.NoProfile) fmt.setDepthBufferSize(0) QSurfaceFormat.setDefaultFormat(fmt) app = QApplication(sys.argv) window = Linhas2D() window.setWindowTitle("Primitivas 2D — GL_LINES") window.resize(600, 400) window.show() return app.exec() if __name__ == "__main__": raise SystemExit(main())