• 十字链表(C语言版本)


    内容其实跟以前写的《十字链表的AOI算法实现》一样的,改一个C语言版本。

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    typedef struct DoubleNode {
        int roleId;
        int x;
        int y;
        struct DoubleNode* xPrev;
        struct DoubleNode* xNext;
        struct DoubleNode* yPrev;
        struct DoubleNode* yNext;
    } DoubleNode;
    
    typedef struct Scene {
        DoubleNode* _head;
        DoubleNode* _tail;
    } Scene;
    
    
    void _add_node(struct Scene* scene, DoubleNode* node);
    void _leave_scene(DoubleNode* node);
    
    // 初始化场景
    struct Scene* new_scene();
    // 进入场景
    DoubleNode* enter_scene(struct Scene* scene, int roleId, int x, int y);
    // 离开场景
    void leave_scene(DoubleNode* node);
    // 场景内移动
    void role_move(struct Scene* scene, DoubleNode* node, int x, int y);
    // 获取AOI
    void get_aoi(struct Scene* scene, DoubleNode* node, int xAreaLen, int yAreaLen);
    // --------------------------------------------------
    
    struct Scene* new_scene() {
        struct Scene* scene = (struct Scene*)malloc(sizeof(struct Scene));
        DoubleNode* _head = (struct DoubleNode*)malloc(sizeof(struct DoubleNode));
        DoubleNode* _tail = (struct DoubleNode*)malloc(sizeof(struct DoubleNode));
    
        _head->roleId = _head->x = _head->y = 0;
        _tail->roleId = _tail->x = _tail->y = 0;
    
        _head->xPrev = _tail->xNext = NULL;
        _head->yPrev = _tail->yNext = NULL;
        _head->xNext = _head->yNext = _tail;
        _tail->xPrev = _tail->yPrev = _head;
    
        scene->_head = _head;
        scene->_tail = _tail;
    
        return scene;
    }
    
    DoubleNode* enter_scene(struct Scene* scene, int roleId, int x, int y) {
        DoubleNode* node = (DoubleNode*)malloc(sizeof(DoubleNode));
        node->roleId = roleId;
        node->x = x;
        node->y = y;
    
        _add_node(scene, node);
        return node;
    }
    
    
    void _add_node(struct Scene* scene, DoubleNode* node) {
        DoubleNode* cur = scene->_head->xNext;
        while(cur != NULL){
            if((cur->x > node->x) || cur == scene->_tail) {
                node->xNext = cur;
                node->xPrev = cur->xPrev;
                cur->xPrev->xNext = node;
                cur->xPrev = node;
                break;
            }
            cur = cur->xNext;
        }
    
        cur = scene->_head->yNext;
        while(cur != NULL){
            if((cur->y > node->y) || cur == scene->_tail) {
                node->yNext = cur;
                node->yPrev = cur->yPrev;
                cur->yPrev->yNext = node;
                cur->yPrev = node;
                break;
            }
            cur = cur->yNext;
        }
    }
    
    void _leave_scene(DoubleNode* node) {
        node->xPrev->xNext = node->xNext;
        node->yPrev->yNext = node->yNext;
        node->xNext->xPrev = node->xPrev;
        node->yNext->yPrev = node->yPrev;
    
        node->xPrev = node->xNext = NULL;
        node->yPrev = node->yNext = NULL;
    }
    
    void leave_scene(DoubleNode* node) {
        _leave_scene(node);
        free(node);
    }
    
    void print_scene(struct Scene* scene) {
        DoubleNode* cur = scene->_head->xNext;
        while(cur != scene->_tail) {
            printf("%d(%d, %d)
    ", cur->roleId, cur->x, cur->y);
            cur = cur->xNext;
        }
    
        printf("
    ");
        cur = scene->_head->yNext;
        while(cur != scene->_tail) {
            printf("%d(%d, %d)
    ", cur->roleId, cur->x, cur->y);
            cur = cur->yNext;
        }
    }
    
    void role_move(struct Scene* scene, DoubleNode* node, int x, int y) {
        _leave_scene(node);
        node->x = x;
        node->y = y;
        _add_node(scene, node);
    }
    
    void get_aoi(struct Scene* scene, DoubleNode* node, int xAreaLen, int yAreaLen) {
        DoubleNode* cur = node->xNext;
        while(cur != scene->_tail){
            if(cur->x < node->x + xAreaLen){
                if(abs(cur->y - node->y) < yAreaLen){
                    printf("aoi = %d(%d, %d)
    ", cur->roleId, cur->x, cur->y);
                }
            }else{
                break;
            }
            cur = cur->xNext;
        }
    
        cur = node->xPrev;
        while(cur != scene->_tail){
            if(node->x < cur->x + xAreaLen){
                if(abs(cur->y - node->y) < yAreaLen){
                    printf("aoi = %d(%d, %d)
    ", cur->roleId, cur->x, cur->y);
                }
            }else{
                break;
            }
            cur = cur->xPrev;
        }
    }
    
    void main() {
        struct Scene* scene = new_scene();
    
        printf("
    == enter_scene == 
    ");
        enter_scene(scene, 1001, 1, 5);
        enter_scene(scene, 1002, 6, 6);
        enter_scene(scene, 1003, 3, 1);
        enter_scene(scene, 1004, 2, 2);
        struct DoubleNode* node1 = enter_scene(scene, 1005, 5, 4);
        struct DoubleNode* node2 = enter_scene(scene, 1006, 3, 3);
    
        print_scene(scene);
    
        printf("
    == leave_scene == 
    ");
        leave_scene(node1);
        print_scene(scene);
    
    
        printf("
    == role_move == 
    ");
        role_move(scene, node2, 2, 2);
        print_scene(scene);
    
        printf("
    == get_aoi == 
    ");
        get_aoi(scene, node2, 2, 2);
    }
    

    作者:Ron Ngai
    出处:http://rondsny.github.io
    关于作者:断码码农一枚。
    欢迎转载,但未经作者同意须在文章页面明显位置给出原文连接
    如有问题,可以通过rondsny#gmail.com 联系我,非常感谢。

  • 相关阅读:
    《JS权威指南学习总结--6.6属性getter和setter》
    《JS权威指南学习总结--6.5枚举属性》
    django的模板系统
    django额外参数的传递和url命名
    python在图片上画矩形
    当爬虫遇到js加密
    php基础-7
    php基础-6
    php基础-5
    php基础-4
  • 原文地址:https://www.cnblogs.com/rond/p/15170814.html
Copyright © 2020-2023  润新知