C++和Java命令行繪制心形圖案
心形線
心形線,是一個圓上的固定一點在它繞著與其相切且半徑相同的另外一個圓周滾動時所形成的軌跡,因其形狀像心形而得名。
心臟線亦為蚶線的一種。在曼德博集合正中間的圖形便是一個心臟線。心臟線的英文名稱“Cardioid”是 de Castillon 在1741年的《Philosophical Transactions of the Royal Society》發(fā)表的;意為“像心臟的”。
極坐標(biāo)方程
水平方向: ρ=a(1-cosθ) 或 ρ=a(1+cosθ) (a>0)
垂直方向: ρ=a(1-sinθ) 或 ρ=a(1+sinθ) (a>0)
直角坐標(biāo)方程
心形線的平面直角坐標(biāo)系方程表達式分別為 x^2+y^2+a*x=a*sqrt(x^2+y^2) 和 x^2+y^2-a*x=a*sqrt(x^2+y^2)
參數(shù)方程
x=a*(2*cos(t)-cos(2*t))
y=a*(2*sin(t)-sin(2*t))
所圍面積為3/2*PI*a^2,形成的弧長為8a
通過不同變換可以有如下樣式
解題思路
在直角坐標(biāo)系中x、y軸的正方向分別是右側(cè)和上方,原點在中間;而在命令行中正方向分別是右方和下方,原點在左上角。因此就需要進行坐標(biāo)軸變換。
由于直角坐標(biāo)系中的心形線是橫著的,因此需要x<->y軸的變換。
由于在命令行具有行高這一固定參數(shù),因此同樣字符數(shù)的行和列長度是不同的(行會比列短很多),因此又需要進行控制臺x軸的拉伸操作。
C++代碼
#include <iostream> #include <math.h> using namespace std; #define X_DIVIDED_BY_Y 0.5 #define MAX_X (35.0 / X_DIVIDED_BY_Y) #define MAX_Y 35.0 #define THRESHOLD 0.5 #define A 13 char getSentenceChar(const char *sentence, int &index) { while(true) { if (index >= strlen(sentence)) { index = 0; } char c = sentence[index++]; if(' ' == c) { index++; } else { return c; } } } inline float getX(float x) { return (x - MAX_X / 2) * X_DIVIDED_BY_Y; } inline float getY(float y) { return MAX_Y / 7.0 - y; } bool func(float x, float y) { return (pow(x, 2) + pow(y, 2) + A * x - A * sqrt(pow(x, 2) + pow(y, 2))) < THRESHOLD; } void main(int argc, char** argv) { const char *LOVE_SENTENCE = "No rose, no diamond ring, that is the simple and romantic love stories in college. The graduates have to face the approaching of June, a time to farewell their beloved. When their future is confronted with love, which one is more important? What will the lovers do in June?"; int sentenceIndex = 0; for (int y = 0; y <= MAX_Y; y++) { for (int x = 0; x <= MAX_X; x++) { cout<<(func(getY(y), getX(x)) ? getSentenceChar(LOVE_SENTENCE, sentenceIndex) : '.'); } cout<<endl; } }
Java代碼
package com.example.demo; public class BenevolenceDemo { private static final float X_DIVIDED_BY_Y = 0.5f; private static final float MAX_X = 35f / X_DIVIDED_BY_Y; private static final float MAX_Y = 35f; private static final float THRESHOLD = 0.5f; private static final float A = 13; private static final String LOVE_SENTENCE = "No rose, no diamond ring, that is the simple and romantic love stories in college. The graduates have to face the approaching of June, a time to farewell their beloved. When their future is confronted with love, which one is more important? What will the lovers do in June?"; private static int sentenceIndex = 0; private static char getSentenceChar() { while(true) { if (sentenceIndex >= LOVE_SENTENCE.length()) { sentenceIndex = 0; } char c = LOVE_SENTENCE.charAt(sentenceIndex++); if(' ' == c) { sentenceIndex++; } else { return c; } } } public static void main(String[] args) { for (int y = 0; y <= MAX_Y; y++) { for (int x = 0; x <= MAX_X; x++) { System.out.print(func(getY(y), getX(x)) ? getSentenceChar() : '='); } System.out.println(); } } public static final float getX(float x) { return (x - MAX_X / 2) * X_DIVIDED_BY_Y; } public static final float getY(float y) { return MAX_Y / 7f - y; } public static boolean func(float x, float y) { return (Math.pow(x, 2) + Math.pow(y, 2) + A * x - A * Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2))) < THRESHOLD; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot通過Junit實現(xiàn)單元測試過程解析
這篇文章主要介紹了Spring Boot通過Junit實現(xiàn)單元測試過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01Java多線程使用阻塞隊列實現(xiàn)生產(chǎn)者消費者模型詳解
這篇文章主要介紹了Java多線程使用阻塞隊列實現(xiàn)生產(chǎn)者消費者模型詳解,主要講解阻塞隊列的特性、實際開發(fā)中常用的到的生產(chǎn)者消費者模型,以及生產(chǎn)者消費者模型解耦合、削峰填谷的好處,需要的朋友可以參考下2023-07-07SpringBoot+Netty實現(xiàn)簡單聊天室的示例代碼
這篇文章主要介紹了如何利用SpringBoot Netty實現(xiàn)簡單聊天室,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)SpringBoot有一定幫助,感興趣的同學(xué)可以了解一下2022-02-02elasticsearch索引的創(chuàng)建過程index?create邏輯分析
這篇文章主要介紹了elasticsearch索引核心index?create,索引的創(chuàng)建過程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04Java SSM整合開發(fā)統(tǒng)一結(jié)果封裝詳解
這篇文章主要介紹了Java SSM整合開發(fā)實現(xiàn)統(tǒng)一結(jié)果封裝,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08關(guān)于Nacos配置管理的統(tǒng)一配置管理、自動刷新詳解
這篇文章主要介紹了關(guān)于Nacos配置管理的統(tǒng)一配置管理、自動刷新詳解,Nacos是阿里的一個開源產(chǎn)品,是針對微服務(wù)架構(gòu)中的服務(wù)發(fā)現(xiàn)、配置管理、服務(wù)治理的綜合型解決方案,需要的朋友可以參考下2023-05-05