python绘制直线的方法

yipeiwu_com4年前Python基础

本文实例为大家分享了python绘制直线的具体代码,供大家参考,具体内容如下

#!/usr/bin/env python
 
import vtk
 
# 绘制通用方法
def myshow(linepolydata):
 # Now we'll look at it.
 lineMapper = vtk.vtkPolyDataMapper()
 if vtk.VTK_MAJOR_VERSION <= 5:
  lineMapper.SetInput(linepolydata)
 else:
  lineMapper.SetInputData(linepolydata)
  lineMapper.SetScalarRange(0, 2)
 lineActor = vtk.vtkActor()
 lineActor.SetMapper(lineMapper)
 
 # The usual rendering stuff.
 camera = vtk.vtkCamera()
 camera.SetPosition(1, 1, 1)
 camera.SetFocalPoint(0, 0, 0)
 
 renderer = vtk.vtkRenderer()
 renWin = vtk.vtkRenderWindow()
 renWin.AddRenderer(renderer)
 
 iren = vtk.vtkRenderWindowInteractor()
 iren.SetRenderWindow(renWin)
 
 renderer.AddActor(lineActor)
 renderer.SetActiveCamera(camera)
 renderer.ResetCamera()
 renderer.SetBackground(0, 0, 0)
 
 renWin.SetSize(300, 300)
 
 # interact with data
 renWin.Render()
 iren.Start()
 del lineMapper
 del lineActor
 del camera
 del renderer
 del renWin
 del iren
 
 
def main():
 # 直线在三维坐标系中的2个顶点
 x = [(0.0, 0.0, 0.0),(1.0, 0.0, 0.0), (0.0, 1.0, 0.0)]
 
 # We'll create the building blocks of polydata including data attributes.
 linepoly = vtk.vtkPolyData()
 points = vtk.vtkPoints()
 lines = vtk.vtkCellArray()
 scalars = vtk.vtkFloatArray()
 
 for i in range(3):
  points.InsertNextPoint(x[i])
 linepoly.SetPoints(points)
 
 line0 = vtk.vtkLine()
 line0.GetPointIds().SetId(0, 0); # 第二个0表示pts中的origin点
 line0.GetPointIds().SetId(1, 1); # 第二个1表示pts中的p0点
 
 line1 = vtk.vtkLine()
 line1.GetPointIds().SetId(0, 0);
 line1.GetPointIds().SetId(1, 2);
 
 lines.InsertNextCell(line0)
 lines.InsertNextCell(line1)
 linepoly.SetLines(lines);
 
 colors = vtk.vtkUnsignedCharArray()
 colors.SetNumberOfComponents(3);
 red = [255, 0, 0]
 colors.InsertNextTypedTuple(red);
 green = [0, 255, 0]
 colors.InsertNextTypedTuple(green);
 linepoly.GetCellData().SetScalars(colors);
 
 del points
 del lines
 del scalars
 del colors
 myshow(linepoly)
 # Clean up
 del linepoly
 
 
 
main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

详解Python数据分析--Pandas知识点

详解Python数据分析--Pandas知识点

本文主要是总结学习pandas过程中用到的函数和方法, 在此记录, 防止遗忘 1. 重复值的处理 利用drop_duplicates()函数删除数据表中重复多余的记录, 比如删除重复多余...

Python subprocess模块功能与常见用法实例详解

Python subprocess模块功能与常见用法实例详解

本文实例讲述了Python subprocess模块功能与常见用法。分享给大家供大家参考,具体如下: 一、简介 subprocess最早在2.4版本引入。用来生成子进程,并可以通过管道连...

在CMD命令行中运行python脚本的方法

在CMD命令行中运行python脚本的方法

网上给出了各种方法,都无碍乎先切换到Python脚本所在目录,然后输入Python脚本名称并回车,本文这里给出了更简便的方法。 方法一: 进入Python脚本所在的文件夹,shift+右...

Python实例分享:快速查找出被挂马的文件

Python实例分享:快速查找出被挂马的文件

思路 需要实现准备一份未受感染的源代码和一份可能受感染的源代码,然后运行以下脚本,就能找出到底哪些文件被挂马了。 其中,主要是根据比对2份文件的md5值来过滤可能被挂马的文件(确切的说应...

Python编码爬坑指南(必看)

Python编码爬坑指南(必看)

自己最近有在学习python,这实在是一门非常短小精悍的语言,很喜欢这种语言精悍背后又有强大函数库支撑的语言。可是刚接触不久就遇到了让人头疼的关于编码的问题,在网上查了很多资料现在在这里...