根据参数数量生成对象

huangapple go评论71阅读模式
英文:

generate object based on parameters count

问题

我想重构我的代码。我有3个类,它们只有在初始化时所需的点数不同(从2到4)。它们都具有相同的方法名称和数量,但它们的实现方式(真的)不同。

我想要一个类,根据点数,构建适当的对象。我还想能够声明所有贝塞尔曲线的函数原型(使用这个类),例如:

void function(const bezier& curve);

头文件:

#pragma once

#include "glm/vec2.hpp"

struct Custom // 将被删除的临时结构
{
    float distance;
    glm::vec2 point;
};

// 需要找到一种方法,只有一个类适用于所有贝塞尔曲线

class Bezier3
{
public:
    glm::vec2 m_P_0, m_P_1, m_P_2, m_P_3;
    // 计数仅用于调试目的,将被删除
    size_t m_Count = 100;

    Bezier3(const glm::vec2& p_0, const glm::vec2& p_1, const glm::vec2& p_2, const glm::vec2& p_3, const size_t& m_Count);
    ~Bezier3();

    glm::vec2 operator() (const float& t);
    glm::vec2 derivate(const float& t);
    Custom findClosestPoint(const glm::vec2& point, double& start);
};

class Bezier2
{
public:
    glm::vec2 m_P_0, m_P_1, m_P_2;
    size_t m_Count = 100;

    Bezier2(const glm::vec2& p_0, const glm::vec2& p_1, const glm::vec2& p_2, const size_t& m_Count);
    ~Bezier2();

    glm::vec2 operator() (const float& t);
    glm::vec2 derivate(const float& t);
    Custom findClosestPoint(const glm::vec2& point);
};

class Bezier1
{
public:
    glm::vec2 m_P_0, m_P_1;
    size_t m_Count = 100;

    Bezier1(const glm::vec2& p_0, const glm::vec2& p_1, const size_t& m_Count);
    ~Bezier1();

    glm::vec2 operator() (const float& t);
    glm::vec2 derivate(const float& t);
    Custom findClosestPoint(const glm::vec2& point);
};

这是可能的吗?我进行了一些研究,但找不到任何信息。

英文:

I would like to refactor my code. I have 3 classes that differ only by the number of points they take to be initialized (from 2 to 4). They all have the same method name and number, but their implementation is (really) different.

I would like to have a class that, depending on the number of points, constructs the appropriate object. I would also like to be able to declare function prototypes (using this class) for all bézier curves, eg:

void function(const bezier& curve);

The header file:

#pragma once

#include "glm/vec2.hpp"

struct Custom // tmp struc will be removed
{
	float distance;
	glm::vec2 point;
};

//need to find a way to have only one class for all of bezier

class Bezier3
{
public:
	glm::vec2 m_P_0, m_P_1, m_P_2, m_P_3;
	// count is for debugging purposes will be removed
	size_t m_Count = 100;

	Bezier3(const glm::vec2& p_0, const glm::vec2& p_1, const glm::vec2& p_2, const glm::vec2& p_3, const size_t& m_Count);
	~Bezier3();

	glm::vec2 operator() (const float& t);
	glm::vec2 derivate(const float& t);
	Custom findClosestPoint(const glm::vec2& point, double& start);

};

class Bezier2
{
public:
	glm::vec2 m_P_0, m_P_1, m_P_2;
	size_t m_Count = 100;

	Bezier2(const glm::vec2& p_0, const glm::vec2& p_1, const glm::vec2& p_2, const size_t& m_Count);
	~Bezier2();

	glm::vec2 operator() (const float& t);
	glm::vec2 derivate(const float& t);
	Custom findClosestPoint(const glm::vec2& point);

};


class Bezier1
{
public:
	glm::vec2 m_P_0, m_P_1;
	size_t m_Count = 100;

	Bezier1(const glm::vec2& p_0, const glm::vec2& p_1, const size_t& m_Count);
	~Bezier1();

	glm::vec2 operator() (const float& t);
	glm::vec2 derivate(const float& t);
	Custom findClosestPoint(const glm::vec2& point);
};

Is this possible? I did some research, but couldn't find anything.

答案1

得分: 1

你可以这样做:

class Bezier
{
public:
  Bezier(const glm::vec2& p_0, const glm::vec2& p_1, const size_t& m_Count) : impl(Bezier1(p_0, p_1, m_Count)) {}
  Bezier(const glm::vec2& p_0, const glm::vec2& p_1, const glm::vec2& p_2, const size_t& m_Count) : impl(Bezier2(p_0, p_1, p_2, m_Count)) {}
  Bezier(const glm::vec2& p_0, const glm::vec2& p_1, const glm::vec2& p_2, const glm::vec2& p_3, const size_t& m_Count) : impl(Bezier3(p_0, p_1, p_2, p_3, m_Count)) {}
  // 其他函数
  // ...
private:
  std::variant<Bezier1, Bezier2, Bezier3> impl;
};

如果你想要在运行时动态决定类型,例如如果你有一个存储 glm::vec2 的向量,你也可以这样做。

英文:

You can do something like this:

class Bezier
{
public:
  Bezier(const glm::vec2&amp; p_0, const glm::vec2&amp; p_1, const size_t&amp; m_Count) : impl(Bezier1(p_0, p_1, m_Count)) {}
  Bezier(const glm::vec2&amp; p_0, const glm::vec2&amp; p_1, const glm::vec2&amp; p_2, const size_t&amp; m_Count) : impl(Bezier2(p_0, p_1, p_2, m_Count)) {}
  Bezier(const glm::vec2&amp; p_0, const glm::vec2&amp; p_1, const glm::vec2&amp; p_2, const glm::vec2&amp; p_3, const size_t&amp; m_Count) : impl(Bezier3(p_0, p_1, p_2, p_3, m_Count)) {}
  // Other functions
  // ...
private:
  std::variant&lt;Bezier1, Bezier2, Bezier3&gt; impl;
};

If you want to decide on type dynamically, e.g. if you have vector of glm::vec2 you can do that too.

huangapple
  • 本文由 发表于 2023年3月4日 07:15:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定