int main(){ node n1(1); node n2(2); node n3(3); node n4(4); node n5(5); node n6(6); node n7(7); node n8(8); node n9(9); node n10(10);
int main() { { /* _______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5 */ Node n1( 6 ); Node n2( 2 ); Node n3( 8 ); Node n4( 0 ); Node n5( 4 ); Node n6( 7 ); Node n7( 9 ); Node n8( 3 ); Node n9( 5 ); n1.left = &n2; n1.right = &n3; n2.left = &n4; n2.right = &n5; n3.left = &n6; n3.right = &n7; n5.left = &n8; n5.right = &n9; const Node * p = lca_bst( &n1, &n8, &n9 ); assert( p == &n5 ); p = lca_bst( &n1, &n2, &n8 ); assert( p == &n2 ); p = lca_bst( &n1, &n7, &n9 ); assert( p == &n1 ); Node n10( 10 ); p = lca_bst( &n1, &n2, &n10 ); assert( p == nullptr ); } return 0; }
void interpolateNormUV( TraceFragment& frag, const Triangle& tri, const Vector2& ab) { ++g_callCnt_interpolate; Vector3 n10(tri.n1 - tri.n0), n20(tri.n2 - tri.n0); Vector2 uv10(tri.uv1 - tri.uv0), uv20(tri.uv2 - tri.uv0); frag.norm = tri.n0; frag.norm += n10 *= ab.x; frag.norm += n20 *= ab.y; frag.norm = frag.norm.normalize(); if (!frag.mat->texture.empty() || !frag.mat->bumpTexture.empty()) { frag.uv = tri.uv0; frag.uv += uv10 *= ab.x; frag.uv += uv20 *= ab.y; } }
int main() { ListNode n6(10); ListNode n7(3); ListNode n8(45); ListNode n9(1); ListNode n10(38); ListNode n11(89); n6.next = &n7; n7.next = &n8; n8.next = &n9; n9.next = &n10; n10.next = &n11; ListNode* n = reverseList(&n6); while(n) { printf("%d->", n->val); n = n->next; } return 0; }
void Cone::tesselate( int nTheta, int nHeight, std::vector< Vector4f >& positions, std::vector< Vector3f >& normals ) { positions.clear(); normals.clear(); positions.reserve( 6 * nTheta * nHeight ); normals.reserve( 6 * nTheta * nHeight ); float dt = MathUtils::TWO_PI / nTheta; float dh = height / nHeight; Vector4f bc( baseCenter, 0 ); for( int t = 0; t < nTheta; ++t ) { float t0 = t * dt; float t1 = t0 + dt; for( int h = 0; h < nHeight; ++h ) { float h0 = h * dh; float h1 = h0 + dh; float r0 = baseRadius * ( 1 - h0 / height ); float r1 = baseRadius * ( 1 - h1 / height ); float x00 = r0 * cosf( t0 ); float y00 = r0 * sinf( t0 ); float x10 = r0 * cosf( t1 ); float y10 = r0 * sinf( t1 ); float x01 = r1 * cosf( t0 ); float y01 = r1 * sinf( t0 ); float x11 = r1 * cosf( t1 ); float y11 = r1 * sinf( t1 ); Vector4f v00 = bc + Vector4f( x00, y00, h0, 1 ); Vector3f n00( x00, y00, r0 / sqrtf( r0 * r0 + h0 * h0 ) ); n00.normalize(); Vector4f v10 = bc + Vector4f( x10, y10, h0, 1 ); Vector3f n10( x10, y10, r0 / sqrtf( r0 * r0 + h0 * h0 ) ); n10.normalize(); Vector4f v01 = bc + Vector4f( x01, y01, h1, 1 ); Vector3f n01( x01, y01, r1 / sqrtf( r1 * r1 + h1 * h1 ) ); n01.normalize(); Vector4f v11 = bc + Vector4f( x11, y11, h1, 1 ); Vector3f n11( x11, y11, r1 / sqrtf( r1 * r1 + h1 * h1 ) ); n11.normalize(); positions.push_back( v00 ); normals.push_back( n00 ); positions.push_back( v10 ); normals.push_back( n10 ); positions.push_back( v01 ); normals.push_back( n01 ); positions.push_back( v01 ); normals.push_back( n01 ); positions.push_back( v10 ); normals.push_back( n10 ); positions.push_back( v11 ); normals.push_back( n11 ); } } }
void Sphere::tesselate( int nTheta, int nPhi, std::vector< Vector4f >& positions, std::vector< Vector3f >& normals ) { positions.clear(); normals.clear(); positions.reserve( 6 * nTheta * nPhi ); normals.reserve( 6 * nTheta * nPhi ); float dt = MathUtils::TWO_PI / nTheta; float dp = MathUtils::PI / nPhi; Vector4f c( center, 0 ); for( int t = 0; t < nTheta; ++t ) { float t0 = t * dt; float t1 = t0 + dt; for( int p = 0; p < nPhi; ++p ) { float p0 = p * dp; float p1 = p0 + dp; float x00 = cosf( t0 ) * sinf( p0 ); float y00 = sinf( t0 ) * sinf( p0 ); float z00 = cosf( p0 ); float x10 = cosf( t1 ) * sinf( p0 ); float y10 = sinf( t1 ) * sinf( p0 ); float z10 = cosf( p0 ); float x01 = cosf( t0 ) * sinf( p1 ); float y01 = sinf( t0 ) * sinf( p1 ); float z01 = cosf( p1 ); float x11 = cosf( t1 ) * sinf( p1 ); float y11 = sinf( t1 ) * sinf( p1 ); float z11 = cosf( p1 ); Vector4f v00 = c + Vector4f( radius * x00, radius * y00, radius * z00, 1 ); Vector3f n00( x00, y00, z00 ); Vector4f v10 = c + Vector4f( radius * x10, radius * y10, radius * z10, 1 ); Vector3f n10( x10, y10, z10 ); Vector4f v01 = c + Vector4f( radius * x01, radius * y01, radius * z01, 1 ); Vector3f n01( x01, y01, z01 ); Vector4f v11 = c + Vector4f( radius * x11, radius * y11, radius * z11, 1 ); Vector3f n11( x11, y11, z11 ); positions.push_back( v00 ); normals.push_back( n00 ); positions.push_back( v10 ); normals.push_back( n10 ); positions.push_back( v01 ); normals.push_back( n01 ); positions.push_back( v01 ); normals.push_back( n01 ); positions.push_back( v10 ); normals.push_back( n10 ); positions.push_back( v11 ); normals.push_back( n11 ); } } }