C++和Java命令行绘制心形图案

所属分类: 软件编程 / java 阅读数: 50
收藏 0 赞 0 分享

心形线

      心形线,是一个圆上的固定一点在它绕着与其相切且半径相同的另外一个圆周滚动时所形成的轨迹,因其形状像心形而得名。

      心脏线亦为蚶线的一种。在曼德博集合正中间的图形便是一个心脏线。心脏线的英文名称“Cardioid”是 de Castillon 在1741年的《Philosophical Transactions of the Royal Society》发表的;意为“像心脏的”。

极坐标方程

水平方向: ρ=a(1-cosθ) 或 ρ=a(1+cosθ) (a>0)
垂直方向: ρ=a(1-sinθ) 或 ρ=a(1+sinθ) (a>0)

直角坐标方程

心形线的平面直角坐标系方程表达式分别为 x^2+y^2+a*x=a*sqrt(x^2+y^2) 和 x^2+y^2-a*x=a*sqrt(x^2+y^2)

参数方程

x=a*(2*cos(t)-cos(2*t))
y=a*(2*sin(t)-sin(2*t))
所围面积为3/2*PI*a^2,形成的弧长为8a

通过不同变换可以有如下样式

解题思路

在直角坐标系中x、y轴的正方向分别是右侧和上方,原点在中间;而在命令行中正方向分别是右方和下方,原点在左上角。因此就需要进行坐标轴变换。

由于直角坐标系中的心形线是横着的,因此需要x<->y轴的变换。

由于在命令行具有行高这一固定参数,因此同样字符数的行和列长度是不同的(行会比列短很多),因此又需要进行控制台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;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

spring boot 静态资源处理方法

本篇文章主要介绍了spring boot 静态资源处理方法。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解spring boot 使用application.properties 进行外部配置

这篇文章主要介绍了详解spring boot 使用application.properties 进行外部配置,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Java实现分页的前台页面和后台代码

这篇文章主要为大家详细介绍了Java实现分页的前台页面和后台代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解spring面向切面aop拦截器

spring中有很多概念和名词,比如过滤器、拦截器、aop等。这篇文章主要介绍了详解spring面向切面aop拦截器,有兴趣的可以了解一下。
收藏 0 赞 0 分享

Java easyui树形表格TreeGrid的实现代码

这篇文章主要为大家详细介绍了Java easyui树形表格TreeGrid的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java的NIO与IO的详解及对比

这篇文章主要介绍了Java的NIO与IO的详解及对比的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中网络IO的实现方式(BIO、NIO、AIO)介绍

这篇文章主要介绍了Java中网络IO的实现方式(BIO、NIO、AIO)介绍的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java 敏感信息加密处理

本文主要介绍了Java 敏感信息加密处理的相关知识:1)敏感信息加密处理我们要实现什么;2)敏感信息加密处理我做了些什么;3)敏感信息加密实现方法。具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享

详解使用Spring3 实现用户登录以及权限认证

这篇文章主要介绍了详解使用Spring3 实现用户登录以及权限认证,这里整理了详细的代码,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

Java的内存机制详解

本文主要介绍了Java的内存机制的相关知识,具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享
查看更多