当前位置: 移动技术网 > IT编程>开发语言>Java > C++和Java命令行绘制心形图案

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

裸色生香,通河大实话,信宜茶山

心形线

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

      心脏线亦为蚶线的一种。在曼德博集合正中间的图形便是一个心脏线。心脏线的英文名称“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;
 }
}

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网