d
.
y
=
a
.
y
-
(
b
.
x
-
a
.
x
)
;
e
.
x
=
d
.
x
+
(
b
.
x
-
a
.
x
-
(
a
.
y
-
b
.
y
)
)
/
2
;
e
.
y
=
d
.
y
-
(
b
.
x
-
a
.
x
+
a
.
y
-
b
.
y
)
/
2
;
if
(
times
>
0
)
{
setcolor
(
rand
(
)
%
15
+
1
)
;
line
(
a
.
x
,
a
.
y
,
b
.
x
,
b
.
y
)
;
line
(
c
.
x
,
c
.
y
,
b
.
x
,
b
.
y
)
;
line
(
c
.
x
,
c
.
y
,
d
.
x
,
d
.
y
)
;
line
(
a
.
x
,
a
.
y
,
d
.
x
,
d
.
y
)
;
pythagorasTree
(
d
,
e
,
times
-
1
)
;
pythagorasTree
(
e
,
c
,
times
-
1
)
;
int
main
(
)
{
point a
,
b
;
double
side
;
int
iter
;
time_t t
;
printf
(
"Enter initial side length : "
)
;
scanf
(
"%lf"
,
&
side
)
;
printf
(
"Enter number of iterations : "
)
;
scanf
(
"%d"
,
&
iter
)
;
a
.
x
=
6
*
side
/
2
-
side
/
2
;
a
.
y
=
4
*
side
;
b
.
x
=
6
*
side
/
2
+
side
/
2
;
b
.
y
=
4
*
side
;
initwindow
(
6
*
side
,
4
*
side
,
"Pythagoras Tree ?"
)
;
srand
(
(
unsigned
)
time
(
&
t
)
)
;
pythagorasTree
(
a
,
b
,
iter
)
;
getch
(
)
;
closegraph
(
)
;
return
0
;
#include <windows.h>
#include <string>
#include <iostream>
const int BMP_SIZE = 720, LINE_LEN = 120, BORDER = 100;
class myBitmap {
public:
myBitmap() : pen( NULL ), brush( NULL ), clr( 0 ), wid( 1 ) {}
~myBitmap() {
DeleteObject( pen ); DeleteObject( brush );
DeleteDC( hdc ); DeleteObject( bmp );
bool create( int w, int h ) {
BITMAPINFO bi;
ZeroMemory( &bi, sizeof( bi ) );
bi.bmiHeader.biSize = sizeof( bi.bmiHeader );
bi.bmiHeader.biBitCount = sizeof( DWORD ) * 8;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biWidth = w;
bi.bmiHeader.biHeight = -h;
HDC dc = GetDC( GetConsoleWindow() );
bmp = CreateDIBSection( dc, &bi, DIB_RGB_COLORS, &pBits, NULL, 0 );
if( !bmp ) return false;
hdc = CreateCompatibleDC( dc );
SelectObject( hdc, bmp );
ReleaseDC( GetConsoleWindow(), dc );
width = w; height = h;
return true;
void clear( BYTE clr = 0 ) {
memset( pBits, clr, width * height * sizeof( DWORD ) );
void setBrushColor( DWORD bClr ) {
if( brush ) DeleteObject( brush );
brush = CreateSolidBrush( bClr );
SelectObject( hdc, brush );
void setPenColor( DWORD c ) {
clr = c; createPen();
void setPenWidth( int w ) {
wid = w; createPen();
void saveBitmap( std::string path ) {
BITMAPFILEHEADER fileheader;
BITMAPINFO infoheader;
BITMAP bitmap;
DWORD wb;
GetObject( bmp, sizeof( bitmap ), &bitmap );
DWORD* dwpBits = new DWORD[bitmap.bmWidth * bitmap.bmHeight];
ZeroMemory( dwpBits, bitmap.bmWidth * bitmap.bmHeight * sizeof( DWORD ) );
ZeroMemory( &infoheader, sizeof( BITMAPINFO ) );
ZeroMemory( &fileheader, sizeof( BITMAPFILEHEADER ) );
infoheader.bmiHeader.biBitCount = sizeof( DWORD ) * 8;
infoheader.bmiHeader.biCompression = BI_RGB;
infoheader.bmiHeader.biPlanes = 1;
infoheader.bmiHeader.biSize = sizeof( infoheader.bmiHeader );
infoheader.bmiHeader.biHeight = bitmap.bmHeight;
infoheader.bmiHeader.biWidth = bitmap.bmWidth;
infoheader.bmiHeader.biSizeImage = bitmap.bmWidth * bitmap.bmHeight * sizeof( DWORD );
fileheader.bfType = 0x4D42;
fileheader.bfOffBits = sizeof( infoheader.bmiHeader ) + sizeof( BITMAPFILEHEADER );
fileheader.bfSize = fileheader.bfOffBits + infoheader.bmiHeader.biSizeImage;
GetDIBits( hdc, bmp, 0, height, ( LPVOID )dwpBits, &infoheader, DIB_RGB_COLORS );
HANDLE file = CreateFile( path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL );
WriteFile( file, &fileheader, sizeof( BITMAPFILEHEADER ), &wb, NULL );
WriteFile( file, &infoheader.bmiHeader, sizeof( infoheader.bmiHeader ), &wb, NULL );
WriteFile( file, dwpBits, bitmap.bmWidth * bitmap.bmHeight * 4, &wb, NULL );
CloseHandle( file );
delete [] dwpBits;
HDC getDC() const { return hdc; }
int getWidth() const { return width; }
int getHeight() const { return height; }
private:
void createPen() {
if( pen ) DeleteObject( pen );
pen = CreatePen( PS_SOLID, wid, clr );
SelectObject( hdc, pen );
HBITMAP bmp; HDC hdc;
HPEN pen; HBRUSH brush;
void *pBits; int width, height, wid;
DWORD clr;
class tree {
public:
tree() {
bmp.create( BMP_SIZE, BMP_SIZE ); bmp.clear();
clr[0] = RGB( 90, 30, 0 ); clr[1] = RGB( 255, 255, 0 );
clr[2] = RGB( 0, 255, 255 ); clr[3] = RGB( 255, 255, 255 );
clr[4] = RGB( 255, 0, 0 ); clr[5] = RGB( 0, 100, 190 );
void draw( int it, POINT a, POINT b ) {
if( !it ) return;
bmp.setPenColor( clr[it % 6] );
POINT df = { b.x - a.x, a.y - b.y }; POINT c = { b.x - df.y, b.y - df.x };
POINT d = { a.x - df.y, a.y - df.x };
POINT e = { d.x + ( ( df.x - df.y ) / 2 ), d.y - ( ( df.x + df.y ) / 2 )};
drawSqr( a, b, c, d ); draw( it - 1, d, e ); draw( it - 1, e, c );
void save( std::string p ) { bmp.saveBitmap( p ); }
private:
void drawSqr( POINT a, POINT b, POINT c, POINT d ) {
HDC dc = bmp.getDC();
MoveToEx( dc, a.x, a.y, NULL );
LineTo( dc, b.x, b.y );
LineTo( dc, c.x, c.y );
LineTo( dc, d.x, d.y );
LineTo( dc, a.x, a.y );
myBitmap bmp;
DWORD clr[6];
int main( int argc, char* argv[] ) {
POINT ptA = { ( BMP_SIZE >> 1 ) - ( LINE_LEN >> 1 ), BMP_SIZE - BORDER },
ptB = { ptA.x + LINE_LEN, ptA.y };
tree t; t.draw( 12, ptA, ptB );
t.save( "?:/pt.bmp" );
return 0;
package main
import (
"image"
"image/color"
"image/draw"
"image/png"
"log"
const (
width, height = 800, 600
maxDepth = 11
colFactor = uint8(255 / maxDepth)
fileName = "pythagorasTree.png"
func main() {
img := image.NewNRGBA(image.Rect(0, 0, width, height))
bg := image.NewUniform(color.RGBA{255, 255, 255, 255})
draw.Draw(img, img.Bounds(), bg, image.ZP, draw.Src)
drawSquares(340, 550, 460, 550, img, 0)
imgFile, err := os.Create(fileName)
if err != nil {
log.Fatal(err)
defer imgFile.Close()
if err := png.Encode(imgFile, img); err != nil {
imgFile.Close()
log.Fatal(err)
func drawSquares(ax, ay, bx, by int, img *image.NRGBA, depth int) {
if depth > maxDepth {
return
dx, dy := bx-ax, ay-by
x3, y3 := bx-dy, by-dx
x4, y4 := ax-dy, ay-dx
x5, y5 := x4+(dx-dy)/2, y4-(dx+dy)/2
col := color.RGBA{0, uint8(depth) * colFactor, 0, 255}
drawLine(ax, ay, bx, by, img, col)
drawLine(bx, by, x3, y3, img, col)
drawLine(x3, y3, x4, y4, img, col)
drawLine(x4, y4, ax, ay, img, col)
drawSquares(x4, y4, x5, y5, img, depth+1)
drawSquares(x5, y5, x3, y3, img, depth+1)
func drawLine(x0, y0, x1, y1 int, img *image.NRGBA, col color.RGBA) {
dx := abs(x1 - x0)
dy := abs(y1 - y0)
var sx, sy int = -1, -1
if x0 < x1 {
sx = 1
if y0 < y1 {
sy = 1
err := dx - dy
for {
img.Set(x0, y0, col)
if x0 == x1 && y0 == y1 {
break
e2 := 2 * err
if e2 > -dy {
err -= dy
x0 += sx
if e2 < dx {
err += dx
y0 += sy
func abs(x int) int {
if x < 0 {
return -x
return x
import java.awt.*;
import java.awt.geom.Path2D;
import javax.swing.*;
public class PythagorasTree extends JPanel {
final int depthLimit = 7;
float hue = 0.15f;
public PythagorasTree() {
setPreferredSize(new Dimension(640, 640));
setBackground(Color.white);
private void drawTree(Graphics2D g, float x1, float y1, float x2, float y2,
int depth) {
if (depth == depthLimit)
return;
float dx = x2 - x1;
float dy = y1 - y2;
float x3 = x2 - dy;
float y3 = y2 - dx;
float x4 = x1 - dy;
float y4 = y1 - dx;
float x5 = x4 + 0.5F * (dx - dy);
float y5 = y4 - 0.5F * (dx + dy);
Path2D square = new Path2D.Float();
square.moveTo(x1, y1);
square.lineTo(x2, y2);
square.lineTo(x3, y3);
square.lineTo(x4, y4);
square.closePath();
g.setColor(Color.getHSBColor(hue + depth * 0.02f, 1, 1));
g.fill(square);
g.setColor(Color.lightGray);
g.draw(square);
Path2D triangle = new Path2D.Float();
triangle.moveTo(x3, y3);
triangle.lineTo(x4, y4);
triangle.lineTo(x5, y5);
triangle.closePath();
g.setColor(Color.getHSBColor(hue + depth * 0.035f, 1, 1));
g.fill(triangle);
g.setColor(Color.lightGray);
g.draw(triangle);
drawTree(g, x4, y4, x5, y5, depth + 1);
drawTree(g, x5, y5, x3, y3, depth + 1);
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawTree((Graphics2D) g, 275, 500, 375, 500, 0);
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Pythagoras Tree");
f.setResizable(false);
f.add(new PythagorasTree(), BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
});
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<style>
canvas {
position: absolute;
top: 45%;
left: 50%;
width: 640px;
height: 640px;
margin: -320px 0 0 -320px;
</style>
</head>
<canvas></canvas>
<script>
'use strict';
var canvas = document.querySelector('canvas');
canvas.width = 640;
canvas.height = 640;
var g = canvas.getContext('2d');
var depthLimit = 7;
var hue = 0.15;
function drawTree(x1, y1, x2, y2, depth) {
if (depth == depthLimit)
return;
var dx = x2 - x1;
var dy = y1 - y2;
var x3 = x2 - dy;
var y3 = y2 - dx;
var x4 = x1 - dy;
var y4 = y1 - dx;
var x5 = x4 + 0.5 * (dx - dy);
var y5 = y4 - 0.5 * (dx + dy);
g.beginPath();
g.moveTo(x1, y1);
g.lineTo(x2, y2);
g.lineTo(x3, y3);
g.lineTo(x4, y4);
g.closePath();
g.fillStyle = HSVtoRGB(hue + depth * 0.02, 1, 1);
g.fill();
g.strokeStyle = "lightGray";
g.stroke();
g.beginPath();
g.moveTo(x3, y3);
g.lineTo(x4, y4);
g.lineTo(x5, y5);
g.closePath();
g.fillStyle = HSVtoRGB(hue + depth * 0.035, 1, 1);
g.fill();
g.strokeStyle = "lightGray";
g.stroke();
drawTree(x4, y4, x5, y5, depth + 1);
drawTree(x5, y5, x3, y3, depth + 1);
function HSVtoRGB(h, s, v) {
var r, g, b, i, f, p, q, t;
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
return "rgb("
+ Math.round(r * 255) + ","
+ Math.round(g * 255) + ","
+ Math.round(b * 255) + ")";
function draw() {
g.clearRect(0, 0, canvas.width, canvas.height);
drawTree(275, 500, 375, 500, 0);
draw();
</script>
</body>
</html>
import java.awt.*
import java.awt.geom.Path2D
import javax.swing.*
class PythagorasTree : JPanel() {
val depthLimit = 7
val hue = 0.15f
init {
preferredSize = Dimension(640, 640)
background = Color.white
private fun drawTree(g: Graphics2D, x1: Float, y1: Float,
x2: Float, y2: Float, depth: Int) {
if (depth == depthLimit) return
val dx = x2 - x1
val dy = y1 - y2
val x3 = x2 - dy
val y3 = y2 - dx
val x4 = x1 - dy
val y4 = y1 - dx
val x5 = x4 + 0.5f * (dx - dy)
val y5 = y4 - 0.5f * (dx + dy)
val square = Path2D.Float()
with (square) {
moveTo(x1, y1)
lineTo(x2, y2)
lineTo(x3, y3)
lineTo(x4, y4)
closePath()
g.color = Color.getHSBColor(hue + depth * 0.02f, 1.0f, 1.0f)
g.fill(square)
g.color = Color.lightGray
g.draw(square)
val triangle = Path2D.Float()
with (triangle) {
moveTo(x3, y3)
lineTo(x4, y4)
lineTo(x5, y5)
closePath()
g.color = Color.getHSBColor(hue + depth * 0.035f, 1.0f, 1.0f)
g.fill(triangle)
g.color = Color.lightGray
g.draw(triangle)
drawTree(g, x4, y4, x5, y5, depth + 1)
drawTree(g, x5, y5, x3, y3, depth + 1)
override fun paintComponent(g: Graphics) {
super.paintComponent(g)
drawTree(g as Graphics2D, 275.0f, 500.0f, 375.0f, 500.0f, 0)
fun main(args: Array<String>) {
SwingUtilities.invokeLater {
val f = JFrame()
with (f) {
defaultCloseOperation = JFrame.EXIT_ON_CLOSE
title = "Pythagoras Tree"
isResizable = false
add(PythagorasTree(), BorderLayout.CENTER)
pack()
setLocationRelativeTo(null);
setVisible(true)
from turtle import goto, pu, pd, color, done
def level(ax, ay, bx, by, depth=0):
if depth > 0:
dx,dy = bx-ax, ay-by
x3,y3 = bx-dy, by-dx
x4,y4 = ax-dy, ay-dx
x5,y5 = x4 + (dx - dy)/2, y4 - (dx + dy)/2
goto(ax, ay), pd()
for x, y in ((bx, by), (x3, y3), (x4, y4), (ax, ay)):
goto(x, y)
pu()
level(x4,y4, x5,y5, depth - 1)
level(x5,y5, x3,y3, depth - 1)
if __name__ == '__main__':
color('red', 'yellow')
pu()
level(-100, 500, 100, 500, depth=8)
done()
C#include<graphics.h>#include<stdlib.h>#include<stdio.h>#include<time.h> typedef struct{ double x,y;}point; void pythagorasTree(point a,point b,int times){ point c...
%thick,scale是整张树形图的大小,可以在0~1内调整树形图的大小
%every node/.style={scale=0.8}是每个节点文字的大小,可以修改调整节点文字的大小。
\begin{tikzpicture} %创建环境
[thick,scale=0.9, every node/.......
对计算机语言越熟悉越是感觉到基础部分的重要性,数理逻辑,数据结构,算法设计与分析,都是越嚼越有味道,这几天一直在看关于递归以及尽量使用递归做东西,发现越是熟悉,越是觉得递归程式的美妙,我们且跨过递归的薄弱部分不谈,就它的优点足以让我兴奋!下面是我用递归实现的两幅图片,一副是毕达哥拉斯树,就是满足毕达哥拉斯定理(勾股定理)的一个分形树,还有一颗自定义树:
毕达哥拉斯树:
以上...
分形几何学是一门以不规则几何形态为研究对象的几何学。一个数学意义上分形的生成是基于一个不断迭代的方程式,即一种基于递归的反馈系统。虽然分形是一个数学构造,它们同样可以在自然界中被找到,这使得它们被划入艺术作品的范畴。
计算机协助了人们推开分形几何的大门。法国数学家曼德尔勃罗特这位计算机和数学兼通的人物,开创了新的数学分支——分形几何学。分形在医学、土力学、地震学和技术分析中都有应用。
毕达哥拉斯树(Pythagoras tree)是由毕达哥拉斯根据勾股定理所画出来的一个可以无限重复的图形。又因为重复数次
在绘制满天星的过程中要运用到turtle工具,它是Python的标准库,也可以形象的称它为海龟库,它可以描绘绘图的轨迹,操作简单、快捷。首先,我们要做一些有关全局的设置
这一步主要是对turtle的画笔大小、绘画延迟以及画布大小进行设置。
绘制一个五角星
绘制满天星的关键就在于如何绘制出一个五角星,接下来通过创建一个有关绘画五角星的函数
上述代码中主要涉及了turtle库的api,在代码注释中已经做了详细的说明,就不再进行赘述了。
前年写了 gcc源码分析,感觉写的不好,如果没有源代码读起来很痛苦,基本上是读天书,这一次改了一种写法,用另一种思路来写,希望这一次能好一点:
1.基本数据结构
编译器前端主要的任务就是把输入的源码转换成一棵语法树,
在gcc中,树的每一个节点用一个结构体来表示,下面就来谈一谈gcc中用到的这个结构体:
union tree_node
Flutter报错The argument type 'Future< String>' can't be assigned to the parameter type 'String'.
35431
android studio报错Error: Gradle project sync failed. Please fix your project and try again.
34130