首先我想说我在编码方面的经验很少。高中时只有几节课。我最近有了为我的 D&D session 开发交互式游戏网格的想法。所以我安装了 Visual Studio 并找到了 Amit 的创建六边形网格的指南:https://www.redblobgames.com/grids/hexagons/
我基本上是在复制代码并尝试在进行过程中理解它。我将首先发布整个程序,然后发布我的编译错误所在的代码片段。
#include "stdafx.h"
#include <cmath>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <iterator>
#include <unordered_set>
#define M_PI 3.14159265358979323846264338327950288
using namespace std;
// Stores hex (cube) coordinates of a cell
struct Hex
{
const int q, r, s;
//Hex(int q_, int r_): q(q_), r(r_), s(-q_ - r_) {}
Hex(int q_, int r_, int s_): q(q_), r(r_), s(s_) {
if (q + r + s != 0) throw "q + r + s must be 0";
}
};
// Stores pixel coordinates
struct Point
{
const double x, y;
Point(double x_, double y_): x(x_), y(y_) {}
};
// Stores fractional hex coordinates when converted from pixel coordinates
struct FractionalHex
{
const double q, r, s;
FractionalHex(double q_, double r_, double s_) : q(q_), r(r_), s(s_) {
if (round(q + r + s) != 0) throw "q + r + s must equal 0";
}
};
// Stores orientation of a cell
struct Orientation
{
const double f0, f1, f2, f3;
const double b0, b1, b2, b3;
const double start_angle; // in multiples of 60deg
Orientation(double f0_, double f1_, double f2_, double f3_, double b0_, double b1_, double b2_, double b3_, double start_angle_): f0(f0_), f1(f1_), f2(f2_), f3(f3_), b0(b0_), b1(b1_), b2(b2_), b3(b3_), start_angle(start_angle_) {}
};
// Stores layout information of a cell
struct Layout
{
const Orientation orientation;
const Point size;
const Point origin;
Layout(Orientation orientation_, Point size_, Point origin_): orientation(orientation_), size(size_), origin(origin_) {}
};
Hex hex_add(Hex a, Hex b)
{
return Hex(a.q + b.q, a.r + b.r, a.s + b.s);
}
Hex hex_subtract(Hex a, Hex b)
{
return Hex(a.q - b.q, a.r - b.r, a.s - b.s);
}
Hex hex_scale(Hex a, int k)
{
return Hex(a.q * k, a.r * k, a.s * k);
}
int hex_length(Hex hex)
{
return int((abs(hex.q) + abs(hex.r) + abs(hex.s)) / 2);
}
int hex_distance(Hex a, Hex b)
{
return hex_length(hex_subtract(a, b));
}
// Rounds fractional hex coordinates with error correction
Hex hex_round(FractionalHex h)
{
int qi = int(round(h.q));
int ri = int(round(h.r));
int si = int(round(h.s));
double q_diff = abs(qi - h.q);
double r_diff = abs(ri - h.r);
double s_diff = abs(si - h.s);
if (q_diff > r_diff && q_diff > s_diff)
{
qi = -ri - si;
}
else
if (r_diff > s_diff)
{
ri = -qi - si;
}
else
{
si = -qi - ri;
}
return Hex(qi, ri, si);
}
// Linearly interpolates (lerp) between two cells
FractionalHex hex_lerp(FractionalHex a, FractionalHex b, double t)
{
return FractionalHex(a.q * (1 - t) + b.q * t, a.r * (1 - t) + b.r * t, a.s * (1 - t) + b.s * t);
}
// Returns all cells in a straight line from a to b
vector<Hex> hex_linedraw(Hex a, Hex b)
{
int N = hex_distance(a, b);
FractionalHex a_nudge = FractionalHex(a.q + 0.000001, a.r + 0.000001, a.s - 0.000002);
FractionalHex b_nudge = FractionalHex(b.q + 0.000001, a.r + 0.000001, a.s - 0.000002);
vector<Hex> results = {};
double step = 1.0 / max(N, 1);
for (int i = 0; i <= N; i++)
{
results.push_back(hex_round(hex_lerp(a_nudge, b_nudge, step * i)));
}
return results;
}
// Returns desired angular direction based on 6 neighboring cells
const vector<Hex> hex_directions =
{
Hex(1, 0, -1), Hex(1, -1, 0), Hex(0, -1, 1), Hex(-1, 0, 1), Hex(-1, 1, 0), Hex(0, 1, -1)
};
Hex hex_direction(int direction)
{
return hex_directions[direction];
}
// Returns neighboring cell in a given direction
Hex hex_neighbor(Hex hex, int direction)
{
return hex_add(hex, hex_direction(direction));
}
// Sets cell orientation to either flat top or pointy top
const Orientation layout_pointy = Orientation(sqrt(3.0), sqrt(3.0) / 2.0, 0.0, 3.0 / 2.0, sqrt(3.0) / 3.0, -1.0 / 3.0, 0.0, 2.0 / 3.0, 0.5);
const Orientation layout_flat = Orientation(3.0 / 2.0, 0.0, sqrt(3.0) / 2.0, sqrt(3.0), 2.0 / 3.0, 0.0, -1.0 / 3.0, sqrt(3.0) / 3.0, 0.0);
// Converts cell coordinates to pixel coordinates
Point hex_to_pixel(Layout layout, Hex h)
{
Orientation M = layout.orientation;
Point size = layout.size;
Point origin = layout.origin;
double x = (M.f0 * h.q + M.f1 * h.r) * size.x;
double y = (M.f2 * h.q + M.f3 * h.r) * size.y;
return Point(x + origin.x, y + origin.y);
}
// Converts pixel coordinates to cell coordinates, use with hex_round function
FractionalHex pixel_to_hex(Layout layout, Point p)
{
Orientation M = layout.orientation;
Point size = layout.size;
Point origin = layout.origin;
Point pt = Point((p.x - origin.x) / size.x, (p.y - origin.y) / size.y);
double q = M.b0 * pt.x + M.b1 * pt.y;
double r = M.b2 * pt.x + M.b3 * pt.y;
return FractionalHex(q, r, -q - r);
}
// Returns pixel coordinates of a cell corner
Point hex_corner_offset(Layout layout, int corner)
{
Orientation M = layout.orientation;
Point size = layout.size;
double angle = 2.0 * M_PI * (M.start_angle - corner) / 6;
return Point(size.x * cos(angle), size.y * sin(angle));
}
// Returns vector of 6 corner coordinates of a cell
vector<Point> polygon_corners(Layout layout, Hex h)
{
vector<Point> corners = {};
Point center = hex_to_pixel(layout, h);
for (int i = 0; i < 6; i++)
{
Point offset = hex_corner_offset(layout, i);
corners.push_back(Point(center.x + offset.x, center.y + offset.y));
}
return corners;
}
// Hash function for Hex, used for map storage
template <> struct hash<Hex>
{
size_t operator()(const Hex& h) const
{
hash<int> int_hash;
size_t hq = int_hash(h.q);
size_t hr = int_hash(h.r);
return hq ^ (hr + 0x9e3779b9 + (hq << 6) + (hq >> 2));
}
};
int main()
{
int map_height = 5;
int map_width = 5;
unordered_set<Hex> map;
for (int q = 0; q < map_height; q++)
{
int q_offset = q >> 1;
for (int s = -q_offset; s < map_width - q_offset; s++)
{
map.insert(Hex(q, -s - q, s));
}
}
}
我有一个编译错误 C2678 二进制“==”:没有找到接受类型为“const Hex”的左侧操作数的运算符(或者没有可接受的转换)
它指向这一行:return hex_directions[direction];
const vector<Hex> hex_directions =
{
Hex(1, 0, -1), Hex(1, -1, 0), Hex(0, -1, 1), Hex(-1, 0, 1), Hex(-1, 1, 0), Hex(0, 1, -1)
};
Hex hex_direction(int direction)
{
return hex_directions[direction];
}
我试着查找错误代码,但我真的不明白如何修复它。任何见解将不胜感激。
最佳答案
嗯,这个错误对我来说很有意义,尽管我实际上认为您可能误读了错误消息的一部分,因为您发布的代码段肯定与 operator== 无关.
您遇到的错误是因为您使用的是 std::unordered_set<Hex> .虽然看起来您已经正确地创建了一个专门的 std::hash<Hex>结构,为了启用散列,std::unordered_set还需要 operator ==重载,以检查散列冲突。
如果您不熟悉,operator==允许您为 Hex 定义“平等”类型。它允许您编写以下代码:
// Check if a hex coordinate is the origin, that is, (0,0,0)
bool is_origin(const Hex& h)
{
return h == Hex{0,0,0};
}
此运算符的一个简单实现如下所示:
bool operator==(const Hex& lhs, const Hex& rhs)
{
return lhs.q == rhs.q && lhs.r == rhs.r;
}
但是,我承认我不熟悉您在这里使用的立方坐标系。我强烈建议查看 this excellent guide到六角坐标系,以确保您能很好地掌握该系统。
关于c++ - 编译十六进制网格程序时错误代码C2768,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50520502/
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
我想用ruby编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R