小编apa*_*ann的帖子

结构与未知大小的结构数组

我一整天都试图绕过这个......

基本上,我有一个名为State的结构,它有一个名称,另一个名为StateMachine,带有名称,状态数组和添加的状态总数:

#include <stdio.h>
#include <stdlib.h>

typedef struct State {
  const char * name;

} State;

typedef struct StateMachine {
  const char * name;

  int total_states;
  State ** states;

} StateMachine;

StateMachine * create_state_machine(const char* name) {
  StateMachine * temp;

  temp = malloc(sizeof(struct StateMachine));

  if (temp == NULL) {
    exit(127);
  }

  temp->name = name;
  temp->total_states = 0;

  temp->states = malloc(sizeof(struct State));
  return temp;
}

void destroy_state_machine(StateMachine* state_machine) {
  free(state_machine);
}

State * add_state(StateMachine* state_machine, const char* name) {
  State * …
Run Code Online (Sandbox Code Playgroud)

c arrays struct

12
推荐指数
2
解决办法
2万
查看次数

线平分线与矩形的交点

我一整天都试图绕过这个......

基本上,我有两个点的坐标,它们总是在一个矩形内.我也知道矩形角的位置.这两个入口点是在运行时给出的.

我需要一个算法来找到2个点,其中由给定点之间的线段产生的平分线与该矩形相交.

图片http://f.cl.ly/items/0o3g1G0F0V120R3S2Q3H/coop.jpg

一些细节:

在上图中,A和B由它们的坐标给出:A(x1,y1)和B(x2,y2).基本上,我需要找到C和D的位置.红色X是AB段的中心.这一点(我们称之为中心)必须在CD线上.

我做了什么:

知道中心和CD斜率给了我CD等式,我试图通过尝试矩形的所有4个边界上的点的位置来找到解决方案. 然而,由于某种原因,它不起作用:每次我有一个解决方案,让我们说C,D绘制在外面,反之亦然.

这是我正在使用的方程式:

从这里,我也可以得到另一个片段(假设我找到了C并且我知道了中心),但是几何失败了我找到这个片段的延伸直到它与矩形的另一边相交.

更新为包含编码代码段

(见主要功能评论)

typedef struct { double x; …
Run Code Online (Sandbox Code Playgroud)

c algorithm geometry 2d computational-geometry

6
推荐指数
1
解决办法
849
查看次数

标签 统计

c ×2

2d ×1

algorithm ×1

arrays ×1

computational-geometry ×1

geometry ×1

struct ×1