参考链接
OpenGL
from OpenGL.GL import *
from OpenGL.GLUT import *
from math import *
import numpy as np
def draw_axes():
glClear(GL_COLOR_BUFFER_BIT)
# 绘制坐标轴
glColor3f(1.0, 1.0, 1.0) # 设置坐标轴颜色为白色
glBegin(GL_LINES)
glVertex2f(-1.0, 0.0) # x 轴
glVertex2f(1.0, 0.0)
glVertex2f(0.0, -1.0) # y 轴
glVertex2f(0.0, 1.0)
# 绘制刻度线
glColor3f(0.5, 0.5, 0.5) # 设置刻度线颜色为灰色
for i in range(-10, 11): # x 轴刻度线
glVertex2f(i * 0.1, -0.02)
glVertex2f(i * 0.1, 0.02)
for i in range(-10, 11): # y 轴刻度线
glVertex2f(-0.02, i * 0.1)
glVertex2f(0.02, i * 0.1)
a1 = np.array([1, 1])
glColor3f(1.0, 1.0, 0.0)
glVertex2f(0.0, 0.0)
glVertex2f(a1[0], a1[1])
glColor3f(1.0, 0.0, 0.0)
theta = 30*pi/180
rotation = np.array([[cos(theta),-sin(theta)],
[sin(theta), cos(theta)]])
a1 = rotation.dot(a1.T)
glVertex2f(0.0, 0.0)
glVertex2f(a1[0], a1[1])
glEnd()
# 添加 X 轴标签
x_label = "X" # X 轴标签内容
glColor3f(1.0, 0.0, 0.0)
draw_text(0.9, -0.1, x_label, GLUT_BITMAP_9_BY_15)
# 添加 Y 轴标签
y_label = "Y" # Y 轴标签内容
glColor3f(0.0, 0.0, 1.0)
draw_text(-0.1, 0.9, y_label, GLUT_BITMAP_9_BY_15)
glColor3f(1.0, 1.0, 1.0)
draw_text(a1[0]-0.2, a1[1]-0.5, "2", GLUT_BITMAP_9_BY_15)
glColor3f(1.0, 1.0, 1.0)
draw_text(a1[0], a1[1]-1, "1", GLUT_BITMAP_9_BY_15)
glFlush()
def draw_text(x, y, text, font):
glRasterPos2f(x, y)
for char in text:
glutBitmapCharacter(font, ord(char))
def main():
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
glutInitWindowSize(400, 400)
glutCreateWindow(b"OpenGL Axes with Tick Marks")
glClearColor(0.0, 0.0, 0.0, 0.0)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-1.0, 1.0, -1.0, 1.0,-1,1)
glutDisplayFunc(draw_axes)
glutMainLoop()
if __name__ == "__main__":
main()