computer-graphics-demo

A demonstration of computer graphics.


Keywords
computer, graphics, computer-graphics, homework, python, ustc
License
GPL-3.0
Install
pip install computer-graphics-demo==0.3rc4

Documentation

computer-graphics-homework

pre-commit.ci status github/workflow codecov FOSSA Status readthedocs

github/downloads github/downloads/latest github/issues github/issues-closed github/issues-pr github/issues-pr-closed github/discussions github/milestones github/forks github/stars github/watchers github/contributors github/commit-activity github/last-commit github/release-date

github/license github/languages github/languages/top github/directory-file-count github/code-size github/repo-size github/v

pypi/downloads pypi/v pypi/format pypi/status pypi/implementation pypi/pyversions

ไธ‰ๆฌกไนฆ้ขไฝœไธš

ๅ„ 10 ๅˆ†๏ผŒๅ…ฑ 30 ๅˆ†๏ผ›ๆŸฅๆ‰พไธ€็ฏ‡ไฝ ็œ‹ไธๆ‡‚็š„่ฎก็ฎ—ๆœบๅ›พๅฝขๅญฆๆ–‡็Œฎ๏ผŒๆ•ด็†ๅ‡บไปฅไธ‹้—ฎ้ข˜

  • ่ฏฅ่ฎบๆ–‡่งฃๅ†ณ็š„้—ฎ้ข˜ๆ˜ฏไป€ไนˆ๏ผŸ
  • ้‡‡็”จ็š„ๆ˜ฏไป€ไนˆๆ–นๆณ•๏ผŸ
  • ๅพ—ๅˆฐไบ†ไป€ไนˆ็ป“่ฎบ

ไธ€ไปฝ็ผ–็จ‹ไฝœไธš

30 ๅˆ†๏ผ›็ฎ—ๆณ•ไปป้€‰๏ผŒไธๅฏ่ฐƒ็”จๅทฒๆœ‰ๅ‡ฝๆ•ฐ๏ผˆ็”ป็‚นๅ‡ฝๆ•ฐ้™คๅค–๏ผ‰๏ผŒๆไบคๅฏๆ‰ง่กŒ็จ‹ๅบๅ’Œๅฎž้ชŒๆŠฅๅ‘Šใ€‚

$ pip install computer-graphics-demo
$ cgdemo --help
Computer graphics algorithms demonstration.

usage: cgdemo [-hVdns] [ [-v] | [-q|-qq] ] [-x <height>] [-y <width>] [-o <out>]
    [-c <color>] [-a <args>] [-t <time>] <command>

options:
    -h, --help              Show this screen.
    -V, --version           Show version.
    -d, --debug             Debug this program.
    -n, --dry-run           Don't show any canvas.
    -s, --second            Use second algorithm. See section algorithm.
    -v, --verbose           Output verbosity. (e.g., default arguments)
    -q, --quiet             Redirect stdout to null. Again for stderr.
    -x, --height <height>   Height of canvas. [default: 512]
    -y, --width <width>     Width of canvas. [default: 1024]
    -o, --output <out>      Save output to a file. See section output.
    -c, --color <color>     Color of graph. [default: 255,0,0]
    -a, --args <args>       Arguments. See section argument.
    -t, --timeout <time>    Auto exit. [default: 30]

commands:
    line                    Draw a line.
    circle                  Draw a circle.
    ellipse                 Draw a ellipse.
    fill                    Fill a region.
    bezier                  Draw a Bezier curve.
    b                       Draw a B-spline.
    koch                    Draw a Koch curve.
    mandelbrot              Draw a Mandelbrot set.
    julia                   Draw a Julia set.
    fern                    Draw a fern.
    reality                 Draw a scene by hidden surface removal, mirror
                            reflection, texture, etc.

output: (%d is format string)
    *%d*.png
    *%d*.jpg
    *%d*.bmp
    */video.mp4
    */video.gif

algorithm:
                default                     second
    line        midpoint                    bresenham
    circle      midpoint                    bresenham
    ellipse     midpoint
    fill        scan                        seed
    bezier      Bezier
    b           De_Boor
    koch        Lindenmayer_system
    mandelbrot  f(z)=z*z+c
    julia       f(z)=z*z+c
    fern        iterated_fuction_system
    reality     ray_tracing

argument:
    line        x1,y1,x2,y2,...
    circle      x0,y0,r
    ellipse     x0,y0,a,b
    fill (scan) x1,y1,x2,y2,x3,y3,...
    fill (seed) x_seed,y_seed,x1,y1,x2,y2,x3,y3,...
    bezier      sample_number,x1,y1,...
    b           degree,x1,y1,...
    koch        scale,iteration_max,x0,y0
    mandelbrot  scale,iteration_max
    julia       scale,iteration_max
    fern        scale,iteration_max,x0,y0
    reality     sample_number,r_aperture

ๅ‚ๆ•ฐไฟฎๆ”น

  • cgdemo -c255,0,0 -a0,0,50,100,100,30 line ๅฏไปฅ็”ปๅ‡บไธ€ๆกไปŽ (0, 0) ๅˆฐ (50, 100) ๅ†ๅˆฐ (100, 30) ็š„็บข่‰ฒ็›ด็บฟใ€‚ๅ…ถไฝ™ 10 ไธชๅ‘ฝไปค็ฑปไผผใ€‚
  • ๅฏนๆœ‰็š„ๅ‘ฝไปคๆไพ›ไบ† 2 ็งไธๅŒ็ฎ—ๆณ•็š„ๅฎž็Žฐ๏ผŒ็”จ -s ๅˆ‡ๆข็ฎ—ๆณ•ใ€‚
  • ๅฏไปฅ็”จ -o ่พ“ๅ‡บๅ›พ็‰‡ๆˆ–่ง†้ข‘ใ€‚ๆณจๆ„ๆ”ฏๆŒ็š„ๆ–‡ไปถๆ ผๅผๅ’Œๆ–‡ไปถๅใ€‚
  • ้ป˜่ฎค้ขœ่‰ฒๅ‡ไธบ็บข่‰ฒ๏ผŒๆœ€ๅŽไธ€ไธชๅ‘ฝไปค้™คๅค–๏ผˆๅ› ไธบๆœ‰ๅคšไธช้ขœ่‰ฒ๏ผŒๆ”นไธบ้šๆœบ๏ผ‰ใ€‚
  • ้ป˜่ฎคๅ‚ๆ•ฐๅฏ้€š่ฟ‡ -v ๆŸฅ็œ‹ใ€‚
  • -n ๅฏไปฅไธๆ‰“ๅผ€็ช—ๅฃใ€‚

ๆณจๆ„ไบ‹้กน

็›ด็บฟๅ…่ฎธๆœ‰ 16 ็งๆƒ…ๅ†ต๏ผˆๆฒกๆœ‰ไฝฟ็”จ 16 ไธชๆกไปถๅˆคๆ–ญ๏ผŒ้‚ฃๆ ท็š„ๅฎž็Žฐๅคช่„ไบ†๏ผ‰

16line

ๆคญๅœ†ๅ…่ฎธๆœ‰ $a = b$ ็ญ‰ 3 ็งๆƒ…ๅ†ตใ€‚

้›ช่Šฑๆ›ฒ็บฟ็š„ๅ‚ๆ•ฐไธญ่ฟญไปฃๆฌกๆ•ฐๅคชๅคงไผšๅ› ไธบ Out of Memory ่ขซ kill ใ€‚

ไธบไบ†ไฟ่ฏ่ทจๅนณๅฐๆ€งๅ’Œๅผ€ๅ‘ๆ•ˆ็Ž‡ไฝฟ็”จ python ๅผ€ๅ‘ใ€‚ๆ‰€ไปฅ็ฌฌไธ€ๆฌก่ฟ่กŒ็ญ‰ๅพ…ๆ—ถ้—ดไผš้•ฟไธ€็‚น๏ผŒ็ฌฌ ไบŒๆฌกไน‹ๅŽไผšๅ› ไธบ็”Ÿๆˆๅญ—่Š‚็ ๅฟซไธ€็‚นใ€‚

ไปฃ็ ็ป“ๆž„

$ exa -T
.
โ”œโ”€โ”€ 1.md // ๅ‰ 3 ๆฌกไฝœไธš็š„ markdown ๆ–‡ไปถ
โ”œโ”€โ”€ 2.md
โ”œโ”€โ”€ 3.md
โ”œโ”€โ”€ docs
โ”‚  โ”œโ”€โ”€ api.rst
โ”‚  โ”œโ”€โ”€ conf.py
โ”‚  โ”œโ”€โ”€ index.rst
โ”‚  โ”œโ”€โ”€ make.bat
โ”‚  โ””โ”€โ”€ Makefile
โ”œโ”€โ”€ images // ๆœฌๆ–‡ไธญๅ‡บ็Žฐ็š„ๆ‰€ๆœ‰ๅ›พ็‰‡
โ”‚  โ”œโ”€โ”€ 16line.png
โ”‚  โ”œโ”€โ”€ b.png
โ”‚  โ”œโ”€โ”€ bezier.png
โ”‚  โ”œโ”€โ”€ circle.png
โ”‚  โ”œโ”€โ”€ ellipse.png
โ”‚  โ”œโ”€โ”€ fern.png
โ”‚  โ”œโ”€โ”€ fill.png
โ”‚  โ”œโ”€โ”€ julia.png
โ”‚  โ”œโ”€โ”€ koch.png
โ”‚  โ”œโ”€โ”€ line.png
โ”‚  โ”œโ”€โ”€ mandelbrot.png
โ”‚  โ””โ”€โ”€ reality.png
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ pyproject.toml
โ”œโ”€โ”€ README.md // ๆœฌๆ–‡
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ scripts
โ”‚  โ””โ”€โ”€ generate_images.sh // ่ฟ่กŒๆญค็จ‹ๅบๅฏไปฅ็”Ÿๆˆๆœฌๆ–‡ไธญๅ‡บ็Žฐ็š„ๆ‰€ๆœ‰ๅ›พ็‰‡
โ”œโ”€โ”€ setup.py
โ””โ”€โ”€ src // ๆ‰€ๆœ‰็ฎ—ๆณ•็š„ๆผ”็คบ๏ผŒๅๅญ—ไธŽ็ฎ—ๆณ•็š„ๅฏนๅบ”ๅ…ณ็ณป่ง `cgdemo --help` ็š„่พ“ๅ‡บ
   โ””โ”€โ”€ computer_graphics_demo
      โ”œโ”€โ”€ __init__.py
      โ”œโ”€โ”€ __main__.py // ไธป็จ‹ๅบ
      โ”œโ”€โ”€ b.py
      โ”œโ”€โ”€ bezier.py
      โ”œโ”€โ”€ circle
      โ”‚  โ”œโ”€โ”€ __init__.py
      โ”‚  โ”œโ”€โ”€ bresenham.py
      โ”‚  โ””โ”€โ”€ midpoint.py
      โ”œโ”€โ”€ ellipse.py
      โ”œโ”€โ”€ fern.py
      โ”œโ”€โ”€ fill
      โ”‚  โ”œโ”€โ”€ __init__.py
      โ”‚  โ”œโ”€โ”€ scan.py
      โ”‚  โ””โ”€โ”€ seed.py
      โ”œโ”€โ”€ julia.py
      โ”œโ”€โ”€ koch.py
      โ”œโ”€โ”€ line
      โ”‚  โ”œโ”€โ”€ __init__.py
      โ”‚  โ”œโ”€โ”€ bresenham.py
      โ”‚  โ””โ”€โ”€ midpoint.py
      โ”œโ”€โ”€ mandelbrot.py
      โ””โ”€โ”€ reality.py

ไธป็จ‹ๅบๅช่ดŸ่ดฃๅ‘ฝไปค่กŒ้€‰้กน็š„่ฏปๅ–ๅ’Œ GUI ็ช—ๅฃ็š„ๅˆๅง‹ๅŒ–๏ผŒๅฎž้™…ไผš่ฐƒ็”จ utils ไธ‹ๅ„ไธชๅญๆจก ๅ—็š„ paint(){.py} ๅฎŒๆˆ็ป˜ๅˆถใ€‚ paint(){.py} ๅฎž็Žฐ็ฎ—ๆณ•๏ผŒๆœ€็ปˆไผš่ฐƒ็”จ setpixel(){.py}, set8pixel(){.py}, set4pixel(){.py} ็ญ‰ๅ‡ฝๆ•ฐใ€‚

ๅ‚่€ƒ็š„ๆฅๆบๅ‡ๅœจๆณจ้‡Šไธญๆ ‡ๅ‡บ๏ผŒไพ‹ๅฆ‚ wikipedia ๅ’ŒๆŸไบ›ๅšๅฎขไธญ C ๆˆ– python ็š„ไปฃ็ ใ€‚

ไปฅไธ‹็”Ÿๆˆ็š„ๅ›พๅฝขๅ‡ๆ˜ฏๅœจ้ป˜่ฎคๅ‚ๆ•ฐไธ‹็”Ÿๆˆ็š„๏ผŒๆฏ”ๅฆ‚ cgdemo line{.sh} ๅฆ‚ๆžœๆƒณไฟฎๆ”นๅ‚ๆ•ฐ็œ‹ ๅ‚ๆ•ฐไฟฎๆ”น๏ผŒไฟฎๆ”นๆŸไบ›ๅ‚ๆ•ฐ้œ€่ฆๆณจๆ„๏ผŒๆฏ”ๅฆ‚้›ช่Šฑๆ›ฒ็บฟ็š„ๅ‚ๆ•ฐไธญ่ฟญไปฃๆฌกๆ•ฐไธ่ฆๅคช ๅคง็ญ‰็ญ‰ใ€‚

ๅ›พๅ…ƒ็š„็”Ÿๆˆ

line

circle

ellipse

fill

ๆ ทๆกๆ›ฒ็บฟ็š„็”Ÿๆˆ

bezier

b

ๅˆ†ๅฝขๅ›พๅฝข็š„็”Ÿๆˆ

koch

mandelbrot

julia

fern

็œŸๅฎžๆ„Ÿๅ›พๅฝข็š„็”Ÿๆˆ

ๆ˜พ็คบไธ€ไธชๅ…ทๆœ‰็œŸๅฎžๆ„Ÿๅœบๆ™ฏ็š„ๅ›พๅฝข๏ผŒๅ‡ ไฝ•้€ ๅž‹่‡ชๅฎšไน‰๏ผŒๅŒ…ๆ‹ฌๆถˆ้šใ€้•œ้ขๅๅฐ„ใ€็บน็†ๆ•ˆๆžœ

reality

ๆœŸๆœซ่€ƒ่ฏ•

40 ๅˆ†