我一整天都试图绕过这个......
基本上,我有一个名为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) 我一整天都试图绕过这个......
基本上,我有两个点的坐标,它们总是在一个矩形内.我也知道矩形角的位置.这两个入口点是在运行时给出的.
我需要一个算法来找到2个点,其中由给定点之间的线段产生的平分线与该矩形相交.
图片http://f.cl.ly/items/0o3g1G0F0V120R3S2Q3H/coop.jpg
一些细节:
在上图中,A和B由它们的坐标给出:A(x1,y1)和B(x2,y2).基本上,我需要找到C和D的位置.红色X是AB段的中心.这一点(我们称之为中心)必须在CD线上.
我做了什么:
找到了中心:
center.x = (A.x+B.x)/2;
center.y = (A.y+B.y)/2;
Run Code Online (Sandbox Code Playgroud)发现CD斜率:
AB_slope = A.y - B.y / A.x - B.x;
CD_slope = -1/AB_slope;
Run Code Online (Sandbox Code Playgroud)知道中心和CD斜率给了我CD等式,我试图通过尝试矩形的所有4个边界上的点的位置来找到解决方案. 然而,由于某种原因,它不起作用:每次我有一个解决方案,让我们说C,D绘制在外面,反之亦然.
这是我正在使用的方程式:
知道x:
y = (CD_slope * (x - center.x)) + center.y;
if y > 0 && y < 512: #=> solution found!
Run Code Online (Sandbox Code Playgroud)知道y:
x = (y - center.y + CD_slope*center.x)/CD_slope;
if x > 0 && x < 512: #=> solution found!
Run Code Online (Sandbox Code Playgroud)从这里,我也可以得到另一个片段(假设我找到了C并且我知道了中心),但是几何失败了我找到这个片段的延伸直到它与矩形的另一边相交.
更新为包含编码代码段
(见主要功能评论)
typedef struct { double x; …Run Code Online (Sandbox Code Playgroud)