最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語(yǔ)言實(shí)現(xiàn)繪制LoveBeat愛(ài)心曲線的示例代碼

 更新時(shí)間:2023年03月08日 09:59:42   作者:編程小魚(yú)六六六  
這篇文章主要為大家詳細(xì)介紹了如何溧陽(yáng)C語(yǔ)言實(shí)現(xiàn)繪制LoveBeat愛(ài)心曲線,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

心形曲線

給出心形曲線參數(shù)方程如下:

x = sin^3(θ)

y = (13 * cos(θ) - 5 * cos(2θ) - 3 * cos(3θ) - cos(4θ)) / 16

對(duì) x, y 同時(shí)乘以半徑 R,即可對(duì)其放大

通過(guò)上述方程可得到若干在曲線上的點(diǎn),記為集合 S

曲線內(nèi)點(diǎn)的生成

對(duì)于內(nèi)部的點(diǎn),我們則將S向內(nèi)擴(kuò)散,使其符合指數(shù)分布,得到S'

令 e ∈ [m, n] 且 e ~ E(λ)

對(duì) P (x, y) ∈ S 作向內(nèi)擴(kuò)散得到點(diǎn) P' ∈ S':

P' = (x, y) * e

擴(kuò)散程度取決于參數(shù) m, n, λ

曲線外點(diǎn)的生成

對(duì)于外部的點(diǎn),我們則將S向外擴(kuò)散,使其符合均勻分布,得到S''

令 u ~ U [1, 1 + b]

對(duì) P (x, y) ∈ S 作向外擴(kuò)散得到點(diǎn) P'' ∈ S'':

P'' = (x, y) * u

擴(kuò)散程度取決于參數(shù) b

制作動(dòng)畫(huà)

有了上述三個(gè)點(diǎn)集合后,就可以描述一個(gè)靜態(tài)的心形圖案

現(xiàn)在,我們?yōu)槠涮砑又芷趧?dòng)畫(huà)效果

首先引入時(shí)間參數(shù) t 和周期函數(shù) T(t) = sin^2(t) (可以是其他周期函數(shù),視效果而定)

對(duì)于 P ∈ S U S'

其周期縮放程度與其到原點(diǎn)的距離 d 成反比,可用 R/d 來(lái)衡量 (R為心形曲線的半徑)

為其添加階數(shù) i 來(lái)擴(kuò)大距離對(duì)縮放程度的影響 (R/d)^i

我們可以得到如下函數(shù)

d' = d * (1 + a * T(t) * (R/d)^i)

外部的點(diǎn)在內(nèi)部點(diǎn)收縮到最小值時(shí),到達(dá)最大值,所以和上式相差一個(gè)相位

我們可以還做一些額外的處理:

  • 對(duì)內(nèi)部點(diǎn)和外部點(diǎn)添加隨機(jī)擾動(dòng)
  • 繪制時(shí),隨機(jī)點(diǎn)的大小
  • 繪制時(shí),隨機(jī)點(diǎn)的顏色、亮度

示例代碼

// 環(huán)境:Visual Studio 2022 C++ 20
// EasyX版本:EasyX 2022-9-1
 
#include <random>
#include <unordered_set>
#include <graphics.h>
#include <cmath>
 
#define PINK LIGHTRED | 0x6055ff
#define LIGHTPINK LIGHTRED | 0x6655ff
#define DRAW(vecs, color) for(auto& vec : vecs)Draw(vec, color, distribution(engine))
 
using namespace std;
 
constexpr float Pi = 3.1416f;
constexpr float Rad = Pi / 180;
constexpr int ScreenWidth = 800;
constexpr int ScreenHeight = 600;
constexpr int OX = ScreenWidth / 2;
constexpr int OY = ScreenHeight / 2;
 
static default_random_engine engine;
 
struct Vec2
{
	float X = .0f;
	float Y = .0f;
 
	Vec2() {}
	Vec2(float x, float y) { X = x; Y = y; }
 
	int GetX() const { return static_cast<int>(X); }
	int GetY() const { return static_cast<int>(Y); }
	
	float Magnitude() const { return sqrt(X * X + Y * Y); }
	Vec2 operator*(float num) const{ return Vec2(X * num, Y * num); }
 
	struct VecHash
	{
		size_t operator()(const Vec2& vec) const noexcept
		{
			return std::hash<float>()(vec.X) ^ std::hash<float>()(vec.Y);
		}
	};
 
	struct VecCompare
	{
		bool operator()(const Vec2& vec1, const Vec2& vec2) const noexcept
		{
			return fabsf(vec1.X - vec2.X) < 1e-2f && fabsf(vec1.Y - vec2.Y) < 1e-2f;
		}
	};
};
using VecSet = unordered_set<Vec2, Vec2::VecHash, Vec2::VecCompare>;
 
 
float CalculateX(float t){ return powf(sin(t), 3.0f); }
float CalculateY(float t){ return -(13 * cosf(t) - 5 * cosf(2 * t) - 2 * cosf(3 * t) - cosf(4 * t)) / 16.0f; }
 
 
VecSet InitHeart(float startAngle, float endAngle, float radius, size_t count)
{
	VecSet set;
	float rad = startAngle * Rad;
	float step = (endAngle - startAngle) * Rad / count;
	float endRad = endAngle * Rad;
 
	for (; rad < endRad; rad += step)
        set.insert(Vec2(CalculateX(rad) * radius, CalculateY(rad) * radius));
 
	return set;
}
 
 
VecSet BuildInside(const VecSet& set, size_t frequency, float lambda, float range, float min)
{
	VecSet retSet;
	exponential_distribution<float> distribution(lambda);
 
	for (size_t i = 0; i < frequency; i++)
	{
		for (auto& vec : set)
		{
			float pX = distribution(engine);
			float scalarX = (pX < 1.0 ? 1.0f - pX : 1.0f) * range + min;
 
			float pY = distribution(engine);
			float scalarY = (pY < 1.0 ? 1.0f - pY : 1.0f) * range + min;
 
			retSet.insert(Vec2(vec.X * scalarX, vec.Y * scalarY));
		}
	}
 
	return retSet;
}
 
 
VecSet BuildOutside(const VecSet& set, size_t frequency, float max)
{
	VecSet retSet;
	uniform_real_distribution<float> distribution(1.0f, max);
 
	for (size_t i = 0; i < frequency; i++)
	{
		for (auto& vec : set)
            retSet.insert(Vec2(vec.X * distribution(engine), vec.Y *  distribution(engine)));
	}
 
	return retSet;
}
 
 
VecSet Undulate(const VecSet& set, float radius)
{
	VecSet retSet;
	uniform_real_distribution<float> distribution(-radius, radius);
 
	for (auto& vec : set)
		retSet.insert(Vec2(vec.X + distribution(engine), vec.Y + distribution(engine)));
 
	return retSet;
}
 
 
VecSet Zoom(const VecSet& set, float factor, float radius, float t, float idx)
{
	VecSet retSet;
 
	for (auto& vec : set)
        retSet.insert(vec * (1.0f + factor * sin(t * Pi) * powf((radius / vec.Magnitude()), idx)));
 
	return retSet;
}
 
 
void Draw(const Vec2& vec, COLORREF color, int radius)
{
	putpixel(vec.GetX() + OX, vec.GetY() + OY, color);
 
	if(radius >= 2)
		putpixel(vec.GetX() + OX + 1, vec.GetY() + OY, color);
 
	if(radius >= 3)
		putpixel(vec.GetX() + OX, vec.GetY() + OY + 1, color);
}
	
 
int main()
{
	float radius = 160.0f;
	auto border = InitHeart(0, 360, radius, 480);
	auto inside = BuildInside(border, 30, 5.0f, 0.85f, 0.15f);
 
	initgraph(ScreenWidth, ScreenHeight);
	BeginBatchDraw();
 
	float t = .0f;
	float tStep = 0.05f;
	uniform_int_distribution<int> distribution(1, 3);
	ExMessage msg{};
 
	while(!peekmessage(&msg, EX_KEY))
	{
		auto ps1 = Zoom(border, 0.1f, radius, t, 1.3f);
		auto ps2 = Undulate(Zoom(inside, 0.1f, radius, t, 1.3f), 3.0f);
		auto ps3 = Undulate(BuildOutside(border, 10 - static_cast<size_t>(sin(t) * 5), 1.35f - sin(t) * 0.15f), 3.0f);
 
		DRAW(ps1, LIGHTPINK);
		DRAW(ps2, LIGHTPINK);
		DRAW(ps3, PINK);
 
		FlushBatchDraw();
		Sleep(40);
 
		t += tStep;
		if (t > 1.0f)
			t = .0f;
 
		cleardevice();
	}
 
	EndBatchDraw();
	closegraph();
 
	return 0;
}

到此這篇關(guān)于C語(yǔ)言實(shí)現(xiàn)繪制LoveBeat愛(ài)心曲線的示例代碼的文章就介紹到這了,更多相關(guān)C語(yǔ)言繪制愛(ài)心曲線內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解如何在code block創(chuàng)建一個(gè)C語(yǔ)言的項(xiàng)目

    詳解如何在code block創(chuàng)建一個(gè)C語(yǔ)言的項(xiàng)目

    這篇文章主要介紹了詳解如何在code block創(chuàng)建一個(gè)C語(yǔ)言的項(xiàng)目,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • C++?Qt開(kāi)發(fā)之使用QUdpSocket實(shí)現(xiàn)組播通信

    C++?Qt開(kāi)發(fā)之使用QUdpSocket實(shí)現(xiàn)組播通信

    Qt?是一個(gè)跨平臺(tái)C++圖形界面開(kāi)發(fā)庫(kù),利用Qt可以快速開(kāi)發(fā)跨平臺(tái)窗體應(yīng)用程序,本文將重點(diǎn)介紹如何運(yùn)用QUdpSocket組件實(shí)現(xiàn)基于UDP的組播通信,感興趣的可以了解下
    2024-03-03
  • C/C++ 監(jiān)控磁盤(pán)與目錄操作的示例

    C/C++ 監(jiān)控磁盤(pán)與目錄操作的示例

    這篇文章主要介紹了C/C++ 監(jiān)控磁盤(pán)與目錄操作的示例,幫助大家更好的理解和學(xué)習(xí)C/C++編程,感興趣的朋友可以了解下
    2020-10-10
  • VS2019創(chuàng)建c++動(dòng)態(tài)鏈接庫(kù)dll與調(diào)用方法實(shí)踐

    VS2019創(chuàng)建c++動(dòng)態(tài)鏈接庫(kù)dll與調(diào)用方法實(shí)踐

    動(dòng)態(tài)鏈接庫(kù)是一個(gè)包含可由多個(gè)程序同時(shí)使用的代碼和數(shù)據(jù)的庫(kù),本文主要介紹了VS2019創(chuàng)建c++動(dòng)態(tài)鏈接庫(kù)dll與調(diào)用方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-06-06
  • C++的static靜態(tài)成員你有了解嗎

    C++的static靜態(tài)成員你有了解嗎

    這篇文章主要為大家詳細(xì)介紹了C++的static靜態(tài)成員,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-02-02
  • C++類(lèi)常量和類(lèi)枚舉

    C++類(lèi)常量和類(lèi)枚舉

    這篇文章主要介紹了C++類(lèi)常量和類(lèi)枚舉,給類(lèi)當(dāng)中定義一些常量,可以給所有類(lèi)的對(duì)象使用,比如說(shuō)我們?cè)陬?lèi)當(dāng)中定義一個(gè)數(shù)組,希望可以定義一個(gè)常量,用來(lái)初始化數(shù)組的長(zhǎng)度,那么下面我i嗎就來(lái)看看過(guò)程當(dāng)如何吧
    2022-01-01
  • C語(yǔ)言中傳值與傳指針的介紹與區(qū)別

    C語(yǔ)言中傳值與傳指針的介紹與區(qū)別

    這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中傳值與傳指針的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C語(yǔ)言具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • 利用C++實(shí)現(xiàn)雙鏈表基本接口示例代碼

    利用C++實(shí)現(xiàn)雙鏈表基本接口示例代碼

    雙鏈表:在單鏈表的每個(gè)結(jié)點(diǎn)中,再設(shè)置一個(gè)指向其前驅(qū)結(jié)點(diǎn)的指針域,下面這篇文章主要給大家介紹了關(guān)于利用C++實(shí)現(xiàn)雙鏈表基本接口的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-08-08
  • C++設(shè)計(jì)模式之職責(zé)鏈模式

    C++設(shè)計(jì)模式之職責(zé)鏈模式

    這篇文章主要介紹了C++設(shè)計(jì)模式之職責(zé)鏈模式,本文講解了什么是職責(zé)鏈模式、什么場(chǎng)合下使用、代碼實(shí)例等內(nèi)容,需要的朋友可以參考下
    2014-10-10
  • C/C++中static,const,inline三種關(guān)鍵字詳細(xì)總結(jié)

    C/C++中static,const,inline三種關(guān)鍵字詳細(xì)總結(jié)

    以下是對(duì)C/C++中static,const,inline的三種關(guān)鍵字進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下
    2013-09-09

最新評(píng)論

樟树市| 顺义区| 安义县| 黑河市| 花莲市| 穆棱市| 新竹县| 莱阳市| 涟源市| 甘孜| 灵丘县| 桦甸市| 偃师市| 崇州市| 融水| 呼和浩特市| 石阡县| 金秀| 和政县| 镇安县| 洛浦县| 上饶县| 石城县| 淮北市| 克东县| 黔东| 建始县| 临泉县| 金塔县| 马尔康县| 民和| 延安市| 闽侯县| 大庆市| 团风县| 依安县| 上犹县| 都江堰市| 庄河市| 仙游县| 永德县|