This tutorials are based on the irrlicht tutorial, show how to create a custom scene node and display it. You have to read the previous tutorials, because the tutorials will skips the knowledgement that we have already talked about before.
create a pyramid, we create a class derived from the irr::scene::ISceneNode class and has to override some methods.
class CSampleSceneNode : public scene::ISceneNode {
core::aabbox3d<f32> Box;//bouding box
video::S3DVertex Vertices[4];//4 vertices
video::SMaterial Material;//materials
public:
CSampleSceneNode(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id)
: scene::ISceneNode(parent, mgr, id)
{
Material.Wireframe = false;
Material.Lighting = false;
// position, normals, and colors, texture
Vertices[0] = video::S3DVertex(0,0,10, 1,1,0,
video::SColor(255,0,255,255), 0, 1);
Vertices[1] = video::S3DVertex(10,0,-10, 1,0,0,
video::SColor(255,255,0,255), 1, 1);
Vertices[2] = video::S3DVertex(0,20,0, 0,1,1,
video::SColor(255,255,255,0), 1, 0);
Vertices[3] = video::S3DVertex(-10,0,-10, 0,0,1,
video::SColor(255,0,255,0), 0, 0);
/*
The Irrlicht Engine needs to know the bounding box of a scene node.
It will use it for automatic culling and other things. Hence, we
need to create a bounding box from the 4 vertices we use.
*/
Box.reset(Vertices[0].Pos);
for (s32 i=1; i<4; ++i)
Box.addInternalPoint(Vertices[i].Pos);
}
/*
normal scene nodes render their content one after another, while stencil buffer shadows would like to be drawn after all other scene nodes. And camera or light scene nodes need to be rendered before all other scene nodes (if at all). So here we simply register the scene node to render normally. If we would like to let it be rendered like cameras or light, we would have to call SceneManager->registerNodeForRendering(this, SNRT_LIGHT_AND_CAMERA)
*/
virtual void OnRegisterSceneNode()
{
if (IsVisible)
SceneManager->registerNodeForRendering(this);
ISceneNode::OnRegisterSceneNode();
}
// override render()
virtual void render()
{
u16 indices[] = { 0,2,3, 2,1,3, 1,0,3, 2,0,1 };
video::IVideoDriver* driver = SceneManager->getVideoDriver();
driver->setMaterial(Material);
driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
driver->drawVertexPrimitiveList(&Vertices[0], 4, &indices[0], 4, video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
}
// get private value
virtual const core::aabbox3d<f32>& getBoundingBox() const
{
return Box;
}
virtual u32 getMaterialCount() const
{
return 1;
}
virtual video::SMaterial& getMaterial(u32 i)
{
return Material;
}
}
Then it’s the main part, just like the pevious example, create the device, video driver, and scene manager and camera.
Next, Create the nodes.
CSampleSceneNode *myNode =
new CSampleSceneNode(smgr->getRootSceneNode(), smgr, 666);
// except this, we also show how to create
// sphere, 0.2 is its radius
scene::ISceneNode *sphereNode = smgr->addSphereSceneNode(0.2);
// cube
scene::ISceneNode *cubeNode=smgr->addCubeSceneNode(100.0f);
// cylinder
const IGeometryCreator* igCreator = smgr->getGeometryCreator();
IMesh* cylinder = igCreator->createCylinderMesh(1.0f, 5.0f, 30, video::SColor(255, 255, 255, 255), false);
scene::IMeshSceneNode* cylinderNode = smgr->addMeshSceneNode(cylinder);
Add an animator to the scence node to rotate it
scene::ISceneNodeAnimator* anim =
smgr->createRotationAnimator(core::vector3df(0.8f, 0, 0.8f));
if (anim) {
myNode->addAnimator(anim);
anim->drop();
anim = 0;
}
// when it's done, drop the node
myNode->drop();
myNode=0;
Then draw everything
while (device->run()) {
driver->beginScene(true, true, video::SColor(0, 100, 100, 100));
smgr->drawAll();
driver->endScene();
}
device->drop();
You can get the complete codes from Github
This tutorials are based on the irrlicht tutorial, show how to load a Quake 3 map into the engine, to create a scencenode for optimizing and the speed of rendering, and how to create a user controlled camera. You have to read the previous tutorials, because the tutorials will skips the knowledgement that we have already talked about before.
Like the previous example, We create a device, the difference is that we ask the user to select which video driver to use. You just need to include the
To display the Quake 3 map, we first need to load it. Quake 3 maps are packed into .pk3 files which are nothing else than .zip files. So we add the .pk3 file to our irr::io::IFileSystem. After it was added, we are able to read from the files in that archive as if they are directly stored on the disk.
device->getFileSystem()->addFileArchive("../../media/map-20kdm2.pk3");
Now we can load the mesh by calling irr::scene::ISceneManager::getMesh(). We get a pointer returned to an irr::scene::IAnimatedMesh. As you might know, Quake 3 maps are not really animated, they are only a huge chunk of static geometry with some materials attached. Hence the IAnimatedMesh consists of only one frame, so we get the “first frame” of the “animation”, which is our quake level and create an Octree scene node with it, using irr::scene::ISceneManager::addOctreeSceneNode(). The Octree optimizes the scene a little bit, trying to draw only geometry which is currently visible. An alternative to the Octree would be a irr::scene::IMeshSceneNode, which would always draw the complete geometry of the mesh, without optimization.
IAnimatedMesh* mesh = smgr->getMesh("20kdm2.bsp");
scene::ISceneNode* node = 0;
if (mesh) {
node = smgr->addOctreeSceneNode(mesh->getMesh(0), 0, -1, 1024);
}
if (node) {
node->setPosition(core::vector3df(-1300,-144,-1249));
}
create a camera which behaves like the ones in first person shooter games (FPS)
smgr->addCameraSceneNodeFPS();
The mouse cursor needs not be visible
device->getCursorControl()->setVisible(false);
In the end draw Everything.
You can get the complete codes from Github
Codes for Indeed Tokyo 2017 Online Test, but I forgot the questions, only update the submission.
#include <iostream>
using namespace std;
int getSequence(int a, int b) {
int t = 1;
for (int n=3; n<=10; n++) {
t = b;
b = b+a;
a = t;
}
return b;
}
int main() {
int a, b;
while (cin >> a>> b)
cout << getSequence(a, b) << endl;
return 0;
}
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
bool isSame(int n, int m) {
unordered_map<int, int> map;
while (n>0) {
int k = n%10;
map[k]++;
n = n/10;
}
while (m>0) {
int b = m%10;
if (map[b]==0) return false;
map[b]--;
m = m/10;
}
return true;
}
int getCount(int n) {
int count = 0;
int len = to_string(n).size();
// substract
for (int i=2; i<10; i++) {
int temp = n / i;
if (n % i !=0) continue;
else {
if (to_string(temp).size() < len)
break;
else
if (isSame(temp, n))
count++;
}
}
//multiple
int i=2;
for (; i<10; i++) {
int temp = n * i;
if (to_string(temp).size() > len)
break;
else
if (isSame(temp, n))
count++;
}
return count;
}
int main() {
int n;
while (cin >> n)
cout << getCount(n) << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int dp[105][105];
vector<int> trouble(105, 0);
int getMax(int n) {
int res = trouble[0];
int index = 0;
for (int i=1; i<=n; i++)
if (trouble[i] > res) {
res = trouble[i];
index = i;
}
trouble[index] = 0;
return index;
}
void getPerm(int n) {
vector<int>vec(n+1, 0);
unordered_map<int, int> map;
int index = getMax(n);
for (int i=1; i<105 && index!=0; i++) {
for (int j=1; j<=n; j++) {
if (dp[j][index]==0) {
if (vec[j]!=0) continue;
vec[j] = index;
map[index]++;
break;
}
}
index = getMax(n);
}
int curr = 1;
for (int k=1; k<=n; k++) {
if (vec[k]!=0)
continue;
while (map[curr]!=0)
curr++;
vec[k] = curr++;
}
for (int i=1; i<=n; i++)
cout << vec[i] << endl;
}
int main() {
int N, M;
cin >> N >> M;
int a, b;
for (int i=0; i<M; i++) {
cin >> a >> b;
dp[a][b] = 1;
trouble[b] += dp[a][b];
}
getPerm(N);
return 0;
}
This tutorials are based on the irrlicht tutorial, show how to set up IDE for suing Irrlicht Engine. Let’s start with “Hello World”.
include the header file, and use irr namespace
#include <irrlicht.h>
using namespace irr;
To use the Irrlicht .DLL file, we need to link with the Irrrlicht.lib, you could do it easily in Visual Studio Property Linker, or you could do the following steps:
#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif
Then it’s our main part.
We create a device.
/* use EDT_SOFTWARE as device type, windows size is (640, 480), amount of color bits per pixel is 16. */
IrrlichtDevice *device =
createDevice( video::EDT_SOFTWARE, dimension2d<u32>(640, 480), 16,
false, false, false, 0);
if (!device)
return 1;
Set the caption of the windows
device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");
Get a pointer to the VideoDriver, the SceneManager and the graphical user interface environment
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IGUIEnvironment* guienv = device->getGUIEnvironment();
Add a hello world label to the window, using the GUI environment. The text is placed at the position (10,10) as top left corner and (260,22) as lower right corner.
guienv->addStaticText(L"Hello World! This is the Irrlicht Software renderer!", rect<s32>(10,10,260,22), true);
In order to get interesting, we use the mesh from the scene manager and then add a scenenode to display it with addAnimatedMeshSceneNode(). In the mean time, check it.
IAnimatedMesh* mesh = smgr->getMesh("../../media/sydney.md2");
if (!mesh)
{
device->drop();
return 1;
}
IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );
Change its material to make it nicer, We disable lighting because we do not have a dynamic light in here, and the mesh would be totally black otherwise. Then we set the frame loop, such that the predefined STAND animation is used. And last, we apply a texture to the mesh.
if (node) {
node -> setMaterialFlag(EMF_LIGHTING, false);
node -> setMD2Animation(scene::EMAT_STAND);
node->setMaterialTexture( 0, driver->getTexture("../../media/sydney.bmp") );
}
Place a camera into the position (0, 30, -40), target is at (0,5,0)
smgr -> addCameraSceneNode(0, vector3df(0, 30, -40), vector3df(0, 5, 0))
Let’s draw Everything.
while(device->run()) {
driver -> beginScene(true, true, SColor(255, 100, 101, 140));
smgr -> drawAll();
guienv -> drawAll();
driver -> endScene();
}
After we are done, we have to delete the Device and every object you created.
device -> drop();
return 0;
You can get the complete codes from Github
Interview on 27. Oct for 朗虹, Some programming tests.
假设股市每秒更新一次数据,即当前的股票价格。现在需要制作一个程序实现实时计算当前一个小时内的股票A的价格参数,即计算这一个小时内的股票价格的平均值和所有时刻的最大值最小值。例如:在11点时计算10点01秒到11点00秒的价格均值和更新全局最大最小值,在11点01秒时计算10点02秒到11点01秒的价格均值和更新全局最大最小值。
The idea is using ring buffer to update.
class PriceParam
{
private:
float average_f; //一小时内的价格均值
float maxPrice_f; //全局最大价格
float minPrice_f; //全局最低价格
float data[3600];
int front;
int rear;
float sum_Now;//update the sum
public:
PriceParam() {
average_f = 0.0;
maxPrice_f = 0.0;
minPrice_f = 0.0;
}
bool isEmpty() {
return front == rear;
}
bool isFull() {
return front == (rear + 1) % 3600;
}
void push(float price) {
if (isFull()) {
int temp = pop();
sum_Now -= temp;
}
data[rear] = price;
rear = (rear + 1) % 3600;
sum_Now += price;
}
int pop() {
int temp = data[front];
front = (front + 1) % 3600;
return temp;
}
void CalculatePriceParam(float currentPrice) {
if (currentPrice > maxPrice_f)
maxPrice_f = currentPrice;
if (currentPrice < minPrice_f)
minPrice_f = currentPrice;
push(currentPrice);
if (isFull())
average_f = sum_Now / 3600;
else
average_f = sum_Now / ((3600-front+rear)%3600);
}
};
int main() {
bool systemRunning = true;
PriceParam* param = new PriceParam();
while (systemRunning)
{
float price;
cin >> price; //从网络获取当前的价格
param->CalculatePriceParam(price);//计算当前的价格参数
}
return 0;
}
有两个有序的数组(从小到大排列), find the medium of two arrays It’s similiar with the Leetcode4
struct MyDataArray{
int *data;
int size;
};
int help(MyDataArray &array1, int start1, MyDataArray &array2, int start2, int k) {
if (array1.size - start1 > array2.size - start2)
return help(array2, start2, array1, start1, k);
if (array1.size == start1)
return array2.data[start2 + k - 1];
if (k == 1)
return min(array1.data[start1], array2.data[start2]);
int pa = min(start1 + k / 2, array1.size);
int pb = start2 + k - pa + start1;
if (array1.data[pa - 1] < array2.data[pb - 1])
return help(array1, pa, array2, start2, k - pa + start1);
else if (array1.data[pa - 1] > array2.data[pb - 1])
return help(array1, start1, array2, pb, k - pb + start2);
else
return array1.data[pa - 1];
}
// complexity is O(log(m+n))
int FindMedium(MyDataArray &array1, MyDataArray &array2) {
int k = array1.size + array2.size;
if (k % 2 == 1)
return help(array1, 0, array2, 0, k / 2 + 1);
else
return (help(array1, 0, array2, 0, k / 2) + help(array1, 0, array2, 0, k / 2 + 1)) / 2;
}
int main() {
MyDataArray array1, array2;
array1.data = new int[5];
array1.size = 5;
for (int i = 0; i < 5; i++)
cin >> array1.data[i];
array2.size = 6;
array2.data = new int[6];
for (int i = 0; i < 6; i++)
cin >> array2.data[i];
int ans = FindMedium(array1, array2);
cout << ans << endl;
return 0;
}
定义 int64_t为8字节的数据类型
//要求将din的第5到第8字节赋值给result.name
//第1个字节赋值给result.score1
//第2个字节赋值给result.score2
//第3个字节赋值给result.score3
//第4个字节赋值给result.score4
struct MyDataStruct
{
char name[4];
unsigned char score1,score2,score3,score4;
};
MyDataStruct GetInformation(int64_t din)
{
MyDataStruct result;
//数值与0xFF按位与运算
//One solution is to use the bit operation
//result.score1 = din & 0x0FF;
//result.score2 = (din>>8) & 0x0FF;
//another solution is to use the pointer
//
result.score1 = *(char*)&din;
result.score2 = *((char*)&din+1);
result.score3 = *((char*)&din + 2);
result.score4 = *((char*)&din + 3);
for (int i = 0; i < 4; i++)
result.name[i] = *((char*)&din + 4 + i);
//如样例中: result.name={0xDD, 0xCC, 0xBB, 0xAA};
//result.score1=0x44 result.score2=0x33
//result.score3=0x22 result.score4=0x11
return result;
}
void main()
{
int64_t var=0xAABBCCDD11223344;
MyDataStruct ans;
ans=GetInformation(var);
}
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root->val < p->val && root->val < q->val)
return lowestCommonAncestor(root->right, p, q);
if (root->val > p->val && root->val > q->val)
return lowestCommonAncestor(root->left, p, q);
return root;
}
};
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (!root || root==p || root == q) return root;
TreeNode* t1 = lowestCommonAncestor(root->left, p, q);
TreeNode* t2 = lowestCommonAncestor(root->right, p, q);
if (t1 && t2) return root;
return t1 ? t1:t2;
}
};