用JAVA和C++ 画心

心形线

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

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

版权声明:转载请注明出处——http://blog.csdn.net/chy555chy/article https://blog.csdn.net/chy555chy/article/details/52743565



还有一种用BufferedImage类来写的

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.Random;
import java.math.BigDecimal;
import static java.lang.Math.*;
public class 心形2 {
    long start = System.currentTimeMillis();
    private Frame f = new Frame("心形线");
    //画布大小
    private final int SIZE=600;
    //重写paint()
    private MyCanvas area = new MyCanvas();

    private BufferedImage image = new BufferedImage(SIZE,SIZE,BufferedImage.TYPE_INT_RGB);
    private Graphics g = image.getGraphics();

    //循环步长
    private final double STEP = 0.00001;

    private void init(){
        area.setPreferredSize(new Dimension(SIZE,SIZE));
        // 画白色背景
        g.setColor(Color.white);
        g.fillRect(0, 0, SIZE, SIZE);
        // 画两条坐标轴
        g.setColor(Color.black);
        g.drawLine(0,SIZE/2,SIZE,SIZE/2);
        g.drawLine(SIZE/2,0,SIZE/2,SIZE);

        //计算坐标
        //新坐标
        int x1, y1,x2, y2;
        for (double t = -Math.PI; t < Math.PI; t = t + STEP){
            g.setColor(Color.black);
            x1 = axisSystem(axisX(t));
            y1 = axisSystem(axisY(t));
            x2 = axisSystem(axisX(t+STEP));
            y2 = axisSystem(axisY(t+STEP));
            //原曲线是横向的,为求美观调整了输出把它画成纵向,若画其他函数须修改
            //g.drawLine(x1, y1, x2, y2);
            g.drawLine(y1, SIZE-x1, y2, SIZE-x2);

        }


        //关闭窗口
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });

        long end = System.currentTimeMillis();
        g.setColor(Color.red);
        g.setFont(new Font("Times", Font.BOLD,15));

        g.drawString("步长值:" + BigDecimal.valueOf(STEP) , 20, 30);
        g.drawString("画图所用时间:" + (end - start) + "毫秒", 20, 50);

        area.repaint();
        f.add(area);
        f.pack();
        f.setVisible(true);
    }

    //根据t算出对应的心形线新坐标X值,若画其他函数须修改
    private double axisX(double t){
        return 2*cos(t) - cos(2*t);
    }

    //根据t算出对应的心形线新坐标Y值,若画其他函数须修改
    private double axisY(double t){
        return 2*sin(t) - sin(2*t);
    }

    //把新坐标换算成系统坐标,若画其他函数须修改
    private int axisSystem(double d){
        return (int)((0.5+d/8)*SIZE);
    }

    private class MyCanvas extends Canvas{
        public void paint(Graphics g){
            g.drawImage(image, 0, 0, null);
        }
    }

    public static void main(String[] args){
        new 心形2().init();
    }
}

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值