这个类是球体,也就是一会要显示的球体了。这个类继承于Geometrics类,并实现了自己的碰撞检测,碰撞原理,书上也说的很清楚了啊,大家多看。然后对照代码就明白了。

类定义:

#pragma once
#ifndef __SPHERE_HEADER__
#define __SPHERE_HEADER__ #include "../geometics.h" class Sphere :public Geometrics {
public:
Sphere();
~Sphere();
Sphere(const Point3& center, ldouble radius);
Sphere(const Sphere& sp);
virtual Geometrics* clone() const;
virtual bool hit(const Ray& ray, ldouble& tmin, ShadeRec& sr) const;
Sphere& operator=(const Sphere& sp);
void set_center(const Point3& p);
Point3 get_center() const;
void set_radius(const ldouble rd);
ldouble get_radius() const;
private:
Point3 c;
ldouble r;
};
#endif

类实现:

#include "pch.h"
#include "sphere.h" Sphere::Sphere() :Geometrics(), c(), r(0) {} Sphere::~Sphere() {} Sphere::Sphere(const Point3& center, ldouble radius) : Geometrics(), c(center), r(radius) {} Sphere::Sphere(const Sphere& sp) : Geometrics(sp), c(sp.c), r(sp.r) {} Geometrics* Sphere::clone() const {
return new Sphere(*this);
} bool Sphere::hit(const Ray& ray, ldouble& tmin, ShadeRec& sr) const {
ldouble t;
Vector3 oc = ray.o - c;
ldouble a = ray.d * ray.d,
b = 2.0 * oc * ray.d,
c = oc * oc - r * r,
disc = b * b - 4.0 * a * c;
if (disc >= 0) {
t = (-b - std::sqrt(disc)) / (2.0 * a);
if (t > 0) {
tmin = t;
sr.normal = (oc + t * ray.d) / r;
sr.local_hit_point = ray.o + t * ray.d;
return true;
}
t = (-b + std::sqrt(disc)) / (2.0 * a);
if (t > 0) {
tmin = t;
sr.normal = (oc + t * ray.d) / r;
sr.local_hit_point = ray.o + t * ray.d;
return true;
}
}
return false;
} Sphere& Sphere::operator=(const Sphere& sp) {
if (this == &sp)
return *this;
Geometrics::operator=(sp);
c = sp.c;
r = sp.r;
return *this;
} void Sphere::set_center(const Point3& p) {
c = p;
} Point3 Sphere::get_center() const {
return c;
} void Sphere::set_radius(const ldouble rd) {
r = rd;
} ldouble Sphere::get_radius() const {
return r;
}

  

Sphere类定义的更多相关文章

  1. Python笔记——类定义

    Python笔记——类定义 一.类定义: class <类名>: <语句> 类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性 如果直接使用类名修改其属 ...

  2. 几种常用的JS类定义方法

    几种常用的JS类定义方法   // 方法1 对象直接量var obj1 = {    v1 : "",    get_v1 : function() {        return ...

  3. Js 类定义的几种方式

    提起面向对象我们就能想到类,对象,封装,继承,多态.在<javaScript高级程序设计>(人民邮电出版社,曹力.张欣译.英文名字是:Professional JavaScript for ...

  4. 为什么C++类定义中,数据成员不能被指定为自身类型,但可以是指向自身类型的指针或引用?为什么在类体内可以定义将静态成员声明为其所属类的类型呢 ?

    static的成员变量,不是存储在Bar实例之中的,因而不会有递归定义的问题. 类声明: class Screen: //Screen类的声明 1 类定义: class Screen{ //Scree ...

  5. C++学了这么多年,你也许不知道为什么类定义要放在.h文件,类实现放在cpp文件。它们如何关联?

    原文  http://blog.csdn.net/ithzhang/article/details/8119286 主题 C++  C++学了这么多年你知道为什么定义类时,类的定义放在.h文件中,而类 ...

  6. YTU 2602: 熟悉题型——类设计( 矩形类定义【C++】)

    2602: 熟悉题型--类设计( 矩形类定义[C++]) 时间限制: 1 Sec  内存限制: 128 MB 提交: 183  解决: 119 题目描述 定义一个矩形类,数据成员包括左下角和右上角坐标 ...

  7. Objective-c 类接口 (@interface) (类定义)

    在Objective-c中如何定义一个类呢?我们可以使用下面的格式进行表示: @interface 类名:父类名{ 变量定义; } 方法定义: @end; 下面给出一个实例: @interface P ...

  8. 只能从脚本中调用在类定义上有[ScriptService]属性的Web服务问题的解决方案

    ajax调用webservice中的接口时, 会出现[只能从脚本中调用在类定义上有[ScriptService]属性的...]的异常. 这是因为, 在.net3.5中, 访问web服务, 要对web服 ...

  9. 开涛spring3(12.4) - 零配置 之 12.4 基于Java类定义Bean配置元数据

    12.4  基于Java类定义Bean配置元数据 12.4.1  概述 基于Java类定义Bean配置元数据,其实就是通过Java类定义Spring配置元数据,且直接消除XML配置文件. 基于Java ...

随机推荐

  1. 理解RESTful Api设计

    REST REST(REpresentational State Transfer)是 Roy Fielding 博士于 2000 年在他的博士论文中提出来的一种软件架构风格(一组架构约束条件和原则) ...

  2. Similarity calculation

    推荐算法入门(相似度计算方法大全) 一.协同过滤算法简介 在推荐系统的众多方法之中,基于用户的协同过滤是诞最早的,原理也比较简单.基于协同过滤的推荐算法被广泛的运用在推荐系统中,比如影视推荐.猜你喜欢 ...

  3. ExtJS 布局-Table布局(Table layout)

    更新记录: 2022年6月1日 开始. 2022年6月10日 发布. 1.说明 table布局类似表格,通过指定行列数实现布局. 2.设置布局方法 在父容器中指定 layout: 'table' la ...

  4. 【RocketMQ】Broker服务注册

    Broker注册 在Broker的启动函数中,添加了定时向NameServer进行注册的任务,在启动后延迟10秒向NameServer进行注册,之后定时发送心跳包,关于发送周期,首先从Broker配置 ...

  5. 2 万字 + 30 张图 | 细聊 MySQL undo log、redo log、binlog 有什么用?

    作者:小林coding 计算机八股文网站:https://xiaolincoding.com/ 大家好,我是小林. 从这篇「执行一条 SQL 查询语句,期间发生了什么?」中,我们知道了一条查询语句经历 ...

  6. SAP Grid control( ALV Grid 列表 自定义 按钮)

    ALV 列表和按钮 效果 源代码 PROGRAM bcalvc_tb_menu_with_def_but. *&&&&&&&&& ...

  7. Java获取汉字的大小写拼音码(汉字的拼音首字母)

    import java.io.UnsupportedEncodingException; /** * 获取拼音码 * * @author xmj * */ public class GetPinyin ...

  8. 论文解读(GCC)《Efficient Graph Convolution for Joint Node RepresentationLearning and Clustering》

    论文信息 论文标题:Efficient Graph Convolution for Joint Node RepresentationLearning and Clustering论文作者:Chaki ...

  9. python常见的错误提示处理

    python常见的错误有 NameError变量名错误 IndentationError代码缩进错误 AttributeError对象属性错误 TypeError类型错误 IOError输入输出错误 ...

  10. 上传几张.NET5之后的机器人logo

    上传几张.NET5之后的机器人logo