https://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=207550&extra=&page=1题目没法描述,哪天画个图片再贴上来,和图形有关的,三角形套三角形,问怎么输出一堆点,然后用已知的画线api生成某一depth的图形,不知道以前面经里有没有这道题。然后就是一直没思路,问hint也没什么有用信息,三哥就说写个简单的情况,然后突然想出点思路,然后三哥一直抠细节,问的心烦,一会问这一会问那,根本没法思考,然后看着电脑的时间不多了,心态也不好了,最后代码就写了一点点,心里当时已经是死灰一片,最后和他repeat了一下思路,貌似之前他还没有理解我要干什么,最后问他怎么做,说我on the right track。出来后感觉三哥就是个坑,占用那么多时间说其他的
我估计应该是这个意思:假设给定一个类似于“void drawLine(const Pt& p1, const Pt& p2)”的API,我也是用的recursion.
我想的办法是构建一个类似金字塔的点阵,depth增加的时候插入新的一层点阵并按照规律画线,不过显然层主的这个方法好多了
输入三个顶点和depth. check 1point3acres for more. 输出一堆点 问somehow,用这些点两两连线把depth的图画出来 |
画三角形那道题可不可以理解为level order traversal 的变种,bfs那种的。据我观察,每加深一层,就是把一个当前的三角形分成三个三角形(中间的倒三角形不算,这样画出的边也没有重合)写了一下代码,constructor没有详写
- public class Point {
- int x, y;
- public Point() {}
- }
- public class Triangle {
- Point A, B, C;
- public Triangle(Point A, Point B, Point C){}
- }
- public Point getMid(Point A, Point B) {
- int x = (A.x + B.x) / 2, y = (A.y + B.y) / 2; //不考虑double的情况
- return new Point(x, y);
- }
- public void drawLine(Point A, Point B) {}
- public void drawTri(Triangle tri) {
- drawLine(tri.A, tri.B);
- drawLine(tri.B, tri.C);
- drawLine(tri.A, tri.C);
- }
- public void drawGraph(Point A, Point B, Point C, int depth) {
- Triangle basic = new Triangle(A, B, C);
- Queue<Triangle> q = new LinkedList<Triangle>();
- q.offer(basic);
- for(int i = 0; i < depth; i++) {
- int size = q.size();
- for(int j = 0; j < size; j++) {
- basic = q.poll();
- Point mid_AB = getMid(basic.A, basic.B);
- Point mid_AC = getMid(basic.A, basic.C);
- Point mid_BC = getMid(basic.B, basic.C);
- q.offer(new Triangle(basic.A, mid_AC, mid_AB));
- q.offer(new Triangle(basic.B, mid_BC, mid_AB));
- q.offer(new Triangle(basic.C, mid_BC, mid_AC));
- }. check 1point3acres for more.
- }
- while(!q.isEmpty()) {. check 1point3acres for more.
- drawTri(q.poll());
- }
- }
- class Point(object):
- def __init__(self, x, y):
- self.x = x
- self.y = y
- def draw_line(p1, p2):
- ninja.penup()
- ninja.goto(p1.x, p1.y)
- ninja.pendown()
- ninja.goto(p2.x, p2.y)
- def draw_trangle(p1, p2, p3):
- draw_line(p1, p2)
- draw_line(p2, p3)
- draw_line(p3, p1)
- def dfs(p1, p2, p3, depth):
- draw_trangle(p1, p2, p3)
- if depth:
- m1 = Point((p1.x+p2.x)/2., (p1.y+p2.y)/2.)
- m2 = Point((p2.x+p3.x)/2., (p2.y+p3.y)/2.)
- m3 = Point((p3.x+p1.x)/2., (p3.y+p1.y)/2.)
- depth -= 1
- dfs(p1, m1, m3, depth)
- dfs(p2, m1, m2, depth)
- dfs(p3, m2, m3, depth)
我估计应该是这个意思:假设给定一个类似于“void drawLine(const Pt& p1, const Pt& p2)”的API,我也是用的recursion.
我想的办法是构建一个类似金字塔的点阵,depth增加的时候插入新的一层点阵并按照规律画线,不过显然层主的这个方法好多了
- typedef pair<double,double> Pt;. check 1point3acres for more.
- typedef vector<Pt> Tri;
- typedef vector<Tri> Tris;
- // assumed given API. check 1point3acres for more.
- void drawLine(const Pt& p1, const Pt& p2);
- // utility function to draw a given triangle
- void drawTri(const Tri& tri) {
- for (int i = 0; i < 3; i++) drawLine(tri[i%3], tri[(i+1)%3]);
- }
- // dfs routine to draw central triangle and collect
- // 4 smaller triangles for each big trianlges
- Tris dfs(const Tris& tris) {
- Tris res; -baidu 1point3acres
- for (const auto& t:tris) {
- drawTri(t); Tri tri;
- // construct and draw central triangle
- for (int i = 0; i < 3; i++)
- tri.push_back(Pt((t[i%3].first+t[(i+1)%3].first)/2.0,(t[i%3].second+t[(i+1)%3].second)/2.0));
- drawTri(tri);
- // collect all 4 smaller triangles into result
- res.push_back(tri);
- for (int i = 0; i < 3; i++) {
- res.push_back({t[i], res[0][i], res[0][(i+2)%3]});
- }
- }
- return res;
- }
- . 1point3acres
- // main function to draw triangles with given depth
- void drawTriangles(const Tri& tri, unsigned int depth) {
- Tris tris = { tri };
- while (depth-- > 0) tris = dfs(tris);
- }